0

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

  • Hi Barry, are you new to Magento 2? Please, learn how to create a simple Magento module, learn about the Magento 2 module structure, layout, template. – Khoa TruongDinh Oct 15 at 15:11
  • Hi, Yes I am new to Magento 2 and I guess it shows. As I have this setup in Magento 1 I thought it cannot be too far away in Magento 2. I will look into this, but any initial suggestions will be helpful. – Barry Hughes Oct 15 at 15:37
  • Hi Barry, You can check this module here github.com/KishanSavaliya/… You can get idea how layout, block and templates are working with Magento 2. Hope this will help you. :) – Kishan Savaliya Oct 15 at 17:19
  • 1
    Hi Kishan, I worked through your module link you sent over and I have this working so that's great. This definitely provides me with an understanding. Thank you for your help. – Barry Hughes Oct 16 at 9:27
  • Thanks Barry, You can accept my answer here and up vote if you like my article :) – Kishan Savaliya Oct 16 at 18:22
1

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.

0

You can use Magento\Framework\View\Element\Template class to call phtml files if you do not have any Block class otherwise you can use your Block instead of using this default Template element which is highlighted.

You can use this in your phtml files to call another phtml file

<?php echo $block->getLayout()->createBlock('Magento\Framework\View\Element\Template')->setTemplate('Vendor_Module::template.phtml')->toHtml();?>

You can use this below content in your admin CMS Pages and Blocks to render your phtml template

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

And you can use below content in your XML file

<block class="Magento\Framework\View\Element\Template" name="your_block_name" before="-" template="Vendor_Module::sampledirectory/template.phtml">

link

Hope this will help you! :)

  • Approve my answer and an upvote if this was helpful to you. :) – Kishan Savaliya Oct 18 at 2:20

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.