Magento – Magento 2: How to add PHP code into CMS Block using .phtml file

blockscustom blockmagento2PHP

I have followed the below links to try and get PHP code into CMS Block which links to a .phtml file, with no avail.

Show content of phtml file on static block Magento 2

PHP code in Block

Currently on our M1 website we have the below setup:

Page Content:

{{block type="catalog/product" template="catalog/pages/towbars.phtml"}}

Referencing the links above, I have tried modifying this, however I am unsure exactly what my "block class" should be and the "template".

{{block class="Path\To\Block\Class" template="Namespace_Module::filename.phtml"}}

Can someone please give me a working example of the above code for Magento 2 and the location of the file, etc.

Many Thanks,
Barry

Best Answer

You can use Magento\Framework\View\Element\Template class to call phtml files

{{block class="Magento\Framework\View\Element\Template" template="Vendor_Module::myfiles/myfile.phtml"}}

To call advanced form use Magento\CatalogSearch\Block\Advanced\Form block class.

{{block class="Magento\CatalogSearch\Block\Advanced\Form" name="block_name" template="Magento_CatalogSearch::advanced/form.phtml"}}

I hope it will help for you.