Programmatically setting custom template for block

Permalink
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];
      $galleryBlock->display();
}


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.

 
shahroq replied on at Permalink Reply
shahroq
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';
$autonav->render('templates/my_template');
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

$galleryBlock->display('templates/templatename');


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) {
            return;
        }
        $bv = new BlockView($this);
        $bv->render($view);
    }
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.
ntisithoj replied on at Permalink Reply
ntisithoj
have the same problem as GetSchwifty ... I also tried
search/templates/jw_cloud/view.php
search/templates/jw_cloud


also tried all teh same formats with
$tagcloudBlock->setCustomTemplate( "..." );


Did anyone every figure out how to get this working?
ntisithoj replied on at Permalink Reply
ntisithoj
just an FYI, the setCustomTemplate problem turns out to be something quite unexpected

The code is crashing because the bID is returning null

Here is my testcase
$tcBlock = BlockType::getByHandle("search");
    $tcData = array(
        "bName" => "tagcloud2",
    );
    $page->addBlock($tcBlock, "Sidebar", $tcData);
    echo $tcBlock->bID; // null
    echo $tcBlock->getBlockID(); //null


The problem is setCustomTemplate calls updateBlockInformation and the later expect there to be a bID

$v = [$bName, $bFilename, $dt, $tcBlock->getBlockID()];


Anyone have any insights as to why the bID is never returned?