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:
<?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

mckoenig
View Replies:
andrewjaff replied on at Permalink Reply
andrewjaff
Hi,

I have not used the block template but you can get all the block added in area and can change the layout dynamically.
$a = new Area('Thumbs');
$blocks = $a->getAreaBlocksArray($c);
foreach($blocks as $block){
 //do your thing here
 // for image you can use this
   $tsb = $block->getInstance();
   $thumb = $tsb->getFileObject();
   if($thumb){
   $img_src= $imgHelper->getThumbnail($thumb,198,95,true);
   echo ("<img src='".$img_src->src."'/>");
}


Thanks
Andrew
mckoenig replied on at Permalink Reply
mckoenig
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.
JohntheFish replied on at Permalink Reply
JohntheFish
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.
mckoenig replied on at Permalink Reply
mckoenig
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…

<?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?
mckoenig replied on at Permalink Reply
mckoenig
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.