How to retrieve variable from CMS page in Magento 2
-
11-12-2020 - |
Question
In Magento 1.9.x, we can do it like below
In CMS page, add content:
{{block type="core/template" name="..." my_vars="123,456" template="path-to/test.phtml"}}
Create path-to/test.phtml
:
<?php echo $this->getData('my_vars'); ?>
My question is: how to achieve it in Magento 2?
Hope somebody could say more detail.
Solution
You can use in Magento 2 like this
{{block class="Magento\Framework\View\Element\Template" name="blockname" template="Vendor_Module::test.phtml" my_vars="123,456"}}
Now you can get the data by calling:
$myVar = $block->getData('my_vars');
OR
$myVar = $block->getMyVars();
Note: You should use $block instead of $this
OTHER TIPS
Here you pass value from cms block/page
{{block ... category_id="value here" ... template="test/test.phtml"}}
In test.phtml:
$this->getData('category_id');
you can also get it By
$block->getCategoryId();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange