4

I'm wondering how I can include a .phtml file inside my cms block. I came across Magento directives which say to use the following:

{{block type="core/template" name="poop6" template="Magento_theme::test.phtml"}}

I imagine this would operate like my layouts xml where I do:

<block class="Magento\Framework\View\Element\Template" template="Magento_theme::test.phtml" />

I've found some very simple articles on the block directive, but none explicitly say where to store the file. I surmise that the "template" attribute works the same as the xml. In this case the test.phtml file is...

app/design/frontend/<theme_namespace>/<theme>/Magento_Theme/templates/test.phtml

Given that the xml <block> example above works, does anyone know why my block directive is not working? Is there more involved for this to work?

Sidenote, none of the articles I found say the Magento version, mine is 2.1.2. Also, the content where my directive is remains empty on the front end, so I believe Magento is at least attempting to interpret it and failing silently (nothing in debug logs).

1 Answer 1

5

The answer was to add the class attribute. No example I found online included this so hopefully this helps someone else out.

Here is basic example. When you copy the directive text into the content HTML editor like so, the class attribute is required. Here it is using the default:

{{block class="Magento\Framework\View\Element\Template" template="Magento_Theme::test.phtml"}}

Which will pull the html from

app/design/frontend/<theme_namespace>/<theme>/Magento_Theme/templates/test.phtml

This works on both cms page content and inside cms block content. It should also work in emails but I haven't tested that or anything else.

Custom Module:

With a custom module including a block seems slightly different:

{{block class="Namespace\ModuleName\Block\BlockName" template="Namespace_ModuleName::test.phtml"}}

Pulls the template from:

app/code/Namespace/ModuleName/view/frontend/templates/test.phtml
4
  • 1
    great job! Congrats on figuring out your own problem! This is good to know. Feel free to mark your answer as accepted.
    – tjons
    Commented Dec 9, 2016 at 23:31
  • Thanks! Another note, I believe Magento 1.x accepted the format I originally tried which is why I keep seeing it referenced everywhere.
    – jamil
    Commented Dec 9, 2016 at 23:33
  • 2
    In Magento 2 the "class" attribute has replaced the "type" attribute. Depending on your Magento version you only need one of them Commented Dec 10, 2016 at 9:25
  • @fschmengler good catch, I removed the type attribute from the answer after testing. Thanks!
    – jamil
    Commented Dec 14, 2016 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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