Display blocks from another area or page

Submitted by herent - 7 years ago

Often you want to show the same content in multiple places, but with different templates. Or even with a limited version of the blocks. Global areas work for this but have limitations - for instance you might want to show left sidebar content in a hidden area of a responsive theme that couldn't be edited from the desktop. Or change the custom template for a block type - if you use the same area in two places, both will use the same templates. You can pass in a second argument that's an array of blocks to the area's display function that allows you to do this very easily. Once you have the array of blocks, you can loop over them to remove blocks you don't want to display.

$c = Page::getCurrentPage();
$sourcePage = Page::getByPath("/path/to/page");
$sourceArea = new Area("Source Area");
$sourceBlocks = $sourceArea->getAreaBlocksArray($sourcePage);
$outputArea = new Area("Output Area");
$outputArea->setCustomTemplate("block_handle", "templates/my_template.php");
$outputArea->display($c, $sourceBlocks);
Stats Categories Tags
comments powered by Disqus