4

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.

8

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

  • Thanks firstly. Vendor_Module corresponding to which path? Suppose I have theme 'frontend/Vendor/lumachild', where should I put the template file? It seems can't find the file. – Key Shang Aug 25 '17 at 10:22
  • If you have theme then you should use Magento_Theme. Full code like this: {{block class="Magento\Framework\View\Element\Template" name="blockname" template="Magento_Theme::test.phtml" my_vars="123,456"}} – Prince Patel Aug 25 '17 at 10:27
  • I put the test.phtml in the theme root folder, but it can't be shown. What's the correct path should I put? – Key Shang Aug 25 '17 at 11:24
  • Put test.phtml at this path app/design/frontend/Vendor/yourtheme/Magento_Theme/templates/test.phtml – Prince Patel Aug 25 '17 at 11:33
  • Still not work, use the Magento CE 2.1.7, you could try it. {{block class="Magento\Framework\View\Element\Template" name="blockname" template="Magento_Theme::test.phtml" my_vars="123,456"}} test.phtml at this path app/design/frontend/Vendor/yourtheme/Magento_Theme/templates‌​/test.phtml Maybe lost some steps? – Key Shang Aug 28 '17 at 3:24
0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.