Programmatically setting custom template for block

Hello. I'm developing a custom Page list. This page list displays news from website. The news page has a gallery (Image Slider block). I need to display this Gallery block inside my custom Page list. This is easy. The catch is that I need to display this Gallery block in different theme than it is displayed on the original News page.

foreach ($pages as $page) {
   $blocks = $page->getBlocks('Gallery');
      $galleryBlock = $blocks[0];

This code renders the Gallery block, but with default template. I thought that I could call this function as follows:
$galleryBlock->setCustomTemplate( "homepageTemplate.php" );

but this resets my template globally so homepageTemplate.php will now be displayed on the original page too -- and I need to use homepageTemplate.php only when calling it programmatically with my custom Page list.

View Replies:
shahroq replied on at Permalink Reply
This is a sample code for inserting a block manually into a page:
$autonav = BlockType::getByHandle('autonav');
$autonav->controller->orderBy = 'display_asc';
$autonav->controller->displayPages = 'top';
$autonav->controller->displaySubPages = 'relevant_breadcrumb';
$autonav->controller->displaySubPageLevels = 'enough';
GetSchwifty replied on at Permalink Reply
Thank you. However I don't need to create a new block. I need to display an already set block -- to call an instance of a block which already has data (images).

Or how should I connect my $galleryBlock (instance of gallery) to a new blocktype like you suggested?
rge replied on at Permalink Reply
Hello GetSchwifty,

The display method accepts a view. Wich is passed to the render method. It does not matter how the template is created.

- block/templates/templatename.php
- block/templates/templatename/view.php


You can see this in the core code. If you are using a debugger like Xdebug you can see this more easily because you can jump into function calls.

public function display($view = 'view')
        if ($this->getBlockTypeID() < 1) {
        $bv = new BlockView($this);
GetSchwifty replied on at Permalink Reply
Thank you for your reply. However this does not work for me. I tried
$galleryBlock->display( 'templates/template.php' );
$galleryBlock->display( 'templates/template' );
$galleryBlock->display( 'template.php' );
$galleryBlock->display( 'template' );

and nothing works, the block renders with default template. My custom Image slider is in /application/blocks/image_slider and template is in /application/blocks/image_slider/templates/template.php.
rge replied on at Permalink Reply
which specific version do you use? There is a bug in the early version of 5.7 that can be related to this.