Programmatically setting a custom template to blocks within a stack
Permalink
Hi,
I've got a stack filled with blocks. They are all of the same type. I want this stack to be displayed on the page twice. Once with the default template, once with a custom template.
I've tried the following:
The stack is placed within the area "thumbs". All the "film" blocks use the standard view though. If I add a single "flim" block to the area (without a stack) the custom template is set.
Is there any way to set the template for the elements within the stack programmatically?
Thanks
I've got a stack filled with blocks. They are all of the same type. I want this stack to be displayed on the page twice. Once with the default template, once with a custom template.
I've tried the following:
<?php $a = new Area('Thumbs'); $a->setCustomTemplate('film', 'templates/thumbs/view.php'); $a->display($c); ?>
The stack is placed within the area "thumbs". All the "film" blocks use the standard view though. If I add a single "flim" block to the area (without a stack) the custom template is set.
Is there any way to set the template for the elements within the stack programmatically?
Thanks
Hi Andrew,
thanks for the advice. I've tested it briefly but it didn't work. It seems like as long as those blocks are in a stack you cannot access them like this. I'll test it a bit more though before my answer is final ;)
Regards.
thanks for the advice. I've tested it briefly but it didn't work. It seems like as long as those blocks are in a stack you cannot access them like this. I'll test it a bit more though before my answer is final ;)
Regards.
5.6.3 includes stack copying. In the PRB is an addon that does the same for 5.6.x<3. You could simply copy the stack, edit the templates, and use different stacks.
If that does not fit with your process, maybe you could make the differences between template A and template B switch on an enclosing class, so both templates then become one and the same. Then when you show the stack, just give those you want to show the B look the class that switches everything inside it.
Getting back to code solutions, rather than show the stack using the core stack display, you could create a new stack display block (its effectively what I did inside that part of Universal Content Puller). The block would list the blocks in the stack and then render them one at a time, in the process making its own (your own) decisions about which block templates to use.
If that does not fit with your process, maybe you could make the differences between template A and template B switch on an enclosing class, so both templates then become one and the same. Then when you show the stack, just give those you want to show the B look the class that switches everything inside it.
Getting back to code solutions, rather than show the stack using the core stack display, you could create a new stack display block (its effectively what I did inside that part of Universal Content Puller). The block would list the blocks in the stack and then render them one at a time, in the process making its own (your own) decisions about which block templates to use.
I've now managed to write code that works - but only when you are logged in. This is very strange. Same behavior as if you edit a block within a stack but forget to publish the edits…
Any idea how to make this code work for not logged in users, too?
<?php $stack = Stack::getByName('Movies'); $ax = Area::get($stack, STACKS_AREA_NAME); $ax->setCustomTemplate('film', 'templates/thumbs/view.php'); $ax->display($stack); ?>
Any idea how to make this code work for not logged in users, too?
When switching of block caching it works for any user. Now this solves my problem right here but it doesn't seem to be the best idea to disable block caching from a performance point of view.
I have not used the block template but you can get all the block added in area and can change the layout dynamically.
Thanks
Andrew