Passing Variables to Custom Blocks from Magento CMS Pages
In this post, we’ll explore how to pass variables of type JSON or other data structures to custom blocks directly from your Magento CMS page content area.
The Challenge: Block Parameters vs. Action Methods
By default, when you use a block in Magento’s CMS page content field, it behaves differently than using an action method with parameters. While action methods allow for easy variable passing via the controller, blocks are limited to pre-set attributes and category IDs.
The code snippet you provided demonstrates this limitation:
{{block type="multibanners/multibanners" category_id="9" name="multibanners" alias="multibanners" template="multibanners/multibanners.phtml"}}
Here, only the category_id attribute is available to be passed. This is because Magento parses and processes block attributes in a different way than it handles action method parameters.
Overcoming the Challenge: Block Arguments
To pass custom data to your block from the CMS page content area, you’ll need to use a feature called “block arguments.” Block arguments allow you to define additional parameters that can be passed to your block during rendering.
Here’s how to do this:
-
Define the Argument in Your Block Class:
In your custom block class (e.g.,
Multibanners.php), add an argument definition to your constructor or a method annotated with@arguments:<?php namespace Magento\Multibanners\Block; use Magento\Framework\View\Element\Template; use Magento\Framework\ArgumentsInterface; class Multibanners extends Template implements ArgumentsInterface { private $data = []; public function __construct( Template\Context $context, array $data = [], array $arguments = [] ) { parent::__construct($context); $this->data = $data; $this->setArguments($arguments); } // Other methods... } -
Use the Argument in Your Block Template:
In your block template file (e.g.,
multibanners.phtml), you can access the passed data:<?php if ($this->getData('custom_variable')): ?> <p>Custom variable value: <?= $this->escapeHtml($this->getData('custom_variable')) ?></p> <?php endif; ?> -
Passing Data from CMS Page Content:
Now, in your CMS page content area where you’re using the block (e.g., under Design > Pages), replace your current code:
{{block type="multibanners/multibanners" category_id="9" name="multibanners" alias="multibanners" template="multibanners/multibanners.phtml"}}with this updated version that includes a JSON argument:
<div class="multibanner-block"> {{block type="multibanners/multibanners" category_id="9" name="multibanners" alias="multibanners" template="multibanners/multibanners.phtml" arguments="{'custom_variable': 'JSON_DATA_HERE'"}} </div>Replace
'JSON_DATA_HERE'with your actual JSON data.
Conclusion
While Magento’s CMS page block system has some limitations compared to action methods, it remains a powerful tool for creating dynamic content. By understanding how to use block arguments, you can pass custom data from your CMS page content directly into your block templates.
Remember to always follow best practices for code organization and performance optimization as outlined in the Laravel Company Blog’s Magento Performance Guide.
This approach allows you to maintain a clean separation of concerns while still providing rich, data-driven content within your CMS pages.