Jump to content
alejandro

Pagetable and variables

Recommended Posts

Hello there,

I'm using a pagetable field with several templates and render(), it's possible to pass a variable? for example:

- file "block" where:   $slider = true

- basic-page:   $block->render()

- _main: if ($slider == true) { slider scripts and css } (but $slider doesn't exists and throws an error, as I understand render() just outputs the html markup)

Is there a way to do this?

Thanks, Alejandro.

 

 

Share this post


Link to post
Share on other sites

Could you show us a little bit more of code? I'm having a hard time understanding how the site is rendering.

Share this post


Link to post
Share on other sites

You can pass an array of variables as an $options argument to $page->render().

// Render page
echo $some_page->render(['animal' => 'cat', 'colour' => 'marmalade']);

// In the template file of the rendered page
echo "I have a {$options['colour']} {$options['animal']}.";

See Ryan's post here for more info on $page->render():

 

  • Like 1

Share this post


Link to post
Share on other sites

But I wanted quite the opposite, I mean:

1) The rendered page is a block containing a slider (block-slider), so I declare a variable here, like $slider = true.

2) The render page ("basic-page" for example) renders that page/block

3) I'm using markup regions and in the _main.php file: $slider = true, so it outputs the necessary scripts and styles for the slider to work

Thought it was a good way to optimize page load speed, to only use the scripts/stylesheets needed for each type of rendered block. But I misunderstood how point 2) is actually working.

Share this post


Link to post
Share on other sites

Anyway, it can be done in the render page with:

foreach ($page->blocks as $block) {
	if (in_array($block->template->id, ['77','79'])) {
		$slider = true;
	}
	echo $block->render();
}

So the blocks (using pageable field) with templates 77 and 79, for example, set the variable and it can be used in he appended _main.php file. But it seemed more logical to define it in the block itself.

 

Share this post


Link to post
Share on other sites

You could check ahead for a variable like this:

$sliders = $page->blocks->find('slider=true')

//Check if sliders are active
if($sliders->count){
 //Echo css/js
}

//Continue rendering

 

Share this post


Link to post
Share on other sites

But $slider is not a field in the block template, just a php variable in the template code. I'd need then a new checkbox field in the slider type block just for this.

Share this post


Link to post
Share on other sites
1 hour ago, alejandro said:

But I wanted quite the opposite

You can set variables to $config for this purpose, e.g.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...