Display existing block with different template
Permalink
I figured out a workaround but I'm hoping someone can shed some light on why this won't work.
Goal was to have a block on the home page of the site that was displayed in several other places on the site but with different templates, so when the home page block changed, it updated all those other places also.
(Since the real example might make more sense, this was forhttp://www.stonybrooklodging.com.... If they change the "Featured Cabin" block on the home page, it should change it in the "Stay" drop-down menu also.)
My first try (some error-checking omitted for brevity:
This displayed the block, but using the original home page template, not the menu bar template. Tried several variations, never could make this work and I don't understand why.
What I ended up doing was creating a new instance and copying the content from one controller to the other:
Shouldn't calling Block::display() with a different template work? Not sure if this is intended behavior or if I should file a bug. It's not a caching issue as I turned off caching completely and it did the same thing.
Goal was to have a block on the home page of the site that was displayed in several other places on the site but with different templates, so when the home page block changed, it updated all those other places also.
(Since the real example might make more sense, this was forhttp://www.stonybrooklodging.com.... If they change the "Featured Cabin" block on the home page, it should change it in the "Stay" drop-down menu also.)
My first try (some error-checking omitted for brevity:
$homePage = Page::getByID(1, 'ACTIVE'); $featuredCabinBlocks = $homePage->getBlocks('FeaturedCabin'); $featuredCabinBlocks[0]->display('templates/menu_bar');
This displayed the block, but using the original home page template, not the menu bar template. Tried several variations, never could make this work and I don't understand why.
What I ended up doing was creating a new instance and copying the content from one controller to the other:
$blockType = BlockType::getByHandle('featured_cabin'); $homePage = Page::getByID(1, 'ACTIVE'); $featuredCabinBlocks = $homePage->getBlocks('FeaturedCabin'); $blockType->controller->unitID = $featuredCabinBlocks[0]->getInstance()->unitID; $blockType->render('templates/menu_bar');
Shouldn't calling Block::display() with a different template work? Not sure if this is intended behavior or if I should file a bug. It's not a caching issue as I turned off caching completely and it did the same thing.