alejandro Posted October 31, 2018 Posted October 31, 2018 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.
elabx Posted October 31, 2018 Posted October 31, 2018 Could you show us a little bit more of code? I'm having a hard time understanding how the site is rendering.
Robin S Posted October 31, 2018 Posted October 31, 2018 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(): 1
alejandro Posted November 1, 2018 Author Posted November 1, 2018 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.
alejandro Posted November 1, 2018 Author Posted November 1, 2018 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.
elabx Posted November 1, 2018 Posted November 1, 2018 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
alejandro Posted November 1, 2018 Author Posted November 1, 2018 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.
Robin S Posted November 1, 2018 Posted November 1, 2018 1 hour ago, alejandro said: But I wanted quite the opposite You can set variables to $config for this purpose, e.g.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now