Magento 2 – How to Use PHTML in CMS Static Block

cms-blockmagento2phtml

I want to make a cms static block which should display/render the content of a .phtml file (echo "Test1"). Content of my cms static block:

<div id="something">
     {{block class="Magento\Catalog\Block\Product\View\Description" name="test" template="product/view/test1.phtml" }}
</div>

something like that. Would this work out ?

Location of "test1.phtml" is in vendor\magento\module-catalog\view\frontend\templates\product\view\test1.phtml

Also: what if I change location of "test1.phtml", how can I write it in {{ … }} ?

Best Answer

You can use Magento\Framework\View\Element\Template basic template.

Try this:

<div id="something">
    {{block class="Magento\Framework\View\Element\Template" name="test" template="path/to/my/file.phtml" }}
</div>

This should work.

UPDATE:

If you need to display product information you have to use blocks from prduct view. Example:

<div id="something">
    {{block class="Magento\Catalog\Block\Product\View\Description" name="test" template="path/to/my/file.phtml" }}
</div>

PHTML file position depends on your architecture. If you are not embedding this in a module you could put it in your template.

Example:

<div id="something">
    {{block class="Magento\Catalog\Block\Product\View\Description" name="test" template="Magento_Theme::mytemplate.phtml" }}
</div>

And put your file under: design/frontend/yourtheme/default/Magento_Theme/mytemplate.phtml