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

Kishan Savaliya
7,8451 gold badge14 silver badges29 bronze badges
asked Oct 15, 2019 at 15:07
5
  • 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. Commented Oct 15, 2019 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. Commented Oct 15, 2019 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. :) Commented Oct 15, 2019 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. Commented Oct 16, 2019 at 9:27
  • Thanks Barry, You can accept my answer here and up vote if you like my article :) Commented Oct 16, 2019 at 18:22

2 Answers 2

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.

answered Oct 16, 2019 at 4:48
1

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! :)

answered Oct 16, 2019 at 7:26
1
  • Approve my answer and an upvote if this was helpful to you. :) Commented Oct 18, 2019 at 2:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.