Jump to content

Recommended Posts

Posted

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.

 

 

Posted

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
Posted

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.

Posted

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.

 

Posted

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

 

Posted

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.

Posted
1 hour ago, alejandro said:

But I wanted quite the opposite

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

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...