How do I add a block to a page pragmatically? ... and also set the blocks template at same time

Permalink
I'm not finding any info on how to setup a block with a custom template and add it to a new page (pragmatically via controller).

Here's my code, it sets up the block and adds it to the newly created page...
<?php
  $bt = BlockType::getByHandle('blog_posts');
            $data = array();
            $data['num_posts_to_show'] = 8;
            $data['characters_to_display'] = 250;
            $data['paginate'] = true;
            $data['max_thumb_width'] = 120;
            $data['show_thumbs'] = true;
            $data['order_by'] = 'date_desc';
            $data['show_rss'] = true;
            $data['rss_title'] = $b->getCollectionName() . ' RSS Feed';
            $data['rss_description'] = 'Blog posts';
            $data['show_posts_from'] = $b->getCollectionID();
            $b->addBlock($bt,'Blog Area',$data);
?>

...just can't figure out how to set the blocks template before it's added.

I've only set the custom template for a block with the $block->render('templates/custom'); method ... but in this case I don't want to render the block, I want to add it to a newly created page so that when user goes to page it has block ready for them.

Can't find api on the addBlock method ... anyone done this before?

MrNiceGaius
 
olliephillips replied on at Permalink Reply
olliephillips
I could be misunderdtanding, but there might be a better way to do this.

Could you not hardcode the block (and the render method, so you can pick your view template) into the page_type. That will achieve the same thing; pages of that page type will show your block and your render call will set the view.
MrNiceGaius replied on at Permalink Reply
MrNiceGaius
HI Ollie, thank you, taking a look at what you're suggesting ... I think you're on the money with your approach.

I'll post back and confirm, thanks again for the direction.