Jump to content

Pagetable and variables


alejandro
 Share

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.

 

 

Link to comment
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
Link to comment
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.

Link to comment
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.

 

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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