I have a very basic custom module showing a compact catalog/product list for logged in customers.
The content of category descriptions etc. is being rendered correctly.
But at the top of my template I include a static block using the code
<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('trade-intro')->toHtml();?>
This block is a pagebuilder block but it is rendered as straight HTML which means layout, images etc. are broken
How do I add it to my template in a way that goes through Pagebuilder rendering?
note: for the category description I go through the helper
$this->helper(Magento\Catalog\Helper\Output::class)->categoryAttribute($winemakercategory, $_description, 'description')
and that uses the right context, but I can't find the same for the simple cms block
1 Answer 1
This is just an quick example using objectManager of how you will need to output your block on the template to get the result you want
<?php
/** @var \Magento\Framework\View\Element\Template $block */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Cms\Model\BlockRepository $blockRepository */
$blockRepository = $objectManager->get(\Magento\Cms\Model\BlockRepository::class);
/** @var \Magento\PageBuilder\Model\Template\Filter $pageBuilderFilter */
$pageBuilderFilter = $objectManager->get(\Magento\PageBuilder\Model\Template\Filter::class);
try {
$cmsBlock = $blockRepository->getById('trade-intro');
$content = $cmsBlock->getContent();
// Render Page Builder JSON content as HTML
echo $pageBuilderFilter->filter($content);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
echo '<!-- CMS block not found -->';
}
?>
Use a proper block class in your project.
<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;
use Magento\Cms\Model\BlockRepository;
use Magento\PageBuilder\Model\Template\Filter;
class PageBuilderBlock extends Template
{
protected $blockRepository;
protected $pageBuilderFilter;
public function __construct(
Template\Context $context,
BlockRepository $blockRepository,
Filter $pageBuilderFilter,
array $data = []
) {
parent::__construct($context, $data);
$this->blockRepository = $blockRepository;
$this->pageBuilderFilter = $pageBuilderFilter;
}
public function renderBlock($identifier)
{
try {
$cmsBlock = $this->blockRepository->getById($identifier);
return $this->pageBuilderFilter->filter($cmsBlock->getContent());
} catch (\Exception $e) {
return '<!-- Block not found or error rendering -->';
}
}
}
and then in your template
<?= $block->renderBlock('trade-intro'); ?>
-
Thanks - I though I had used a proper block class, the Block code extends \Magento\Framework\View\Element\Template - what I had missed is that i now need to add this whole pagebuilderfilter - because that's how old this block's been working. Do I need to do it when calling the CMS block or should I do it on the entire custom block's output?iphigenie– iphigenie2025年10月23日 09:55:08 +00:00Commented Oct 23 at 9:55
-
Create a block class for your template and there you can add a function to render your block using pagebuilderfilter and then call it form your template.Nikolas– Nikolas2025年10月23日 10:38:19 +00:00Commented Oct 23 at 10:38
Explore related questions
See similar questions with these tags.