2

Via Admin > Content > Pages I made a page that I will be using as the homepage. I want to add some PHP to this page, however this does not work (directly).

How can I do this?

I remember using a .phtml for this in Magento 1, but I can't remember how I did this, and things may well be changed since Magento 2.

1 Answer 1

3

In your page's content section, you can insert a .phtml template file using the following:

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

This requires you to have a module with the template you want to use, for example: {theme_directory}/Your_Module/template/mytemplate.phtml and then reference it as Your_Module::mytemplate.phtml.

4
  • Thanks! I've added test.phtml in <theme-folder>/Magento_Theme/template. How do I get the block_id (your_block_identifier)?
    – Kevin
    Commented Aug 21, 2016 at 19:50
  • That part is just a unique identifier that you assign to the block. It can be whatever you want but has to be unique to that page. Commented Aug 21, 2016 at 20:33
  • I added the file app/design/frontend/<vendor_name>/<theme_name>/Magento_Theme/template/html/test.phtml. The content of the file is <?php echo "Hello World"; ?>. After running sudo php bin/magento setup:static-content:deploy nl_NL en_US and flushing the cache, I added `{{block class="Magento\Framework\View\Element\Template" block_id="homepage_block" template="Magento_Theme::html/test.phtml"}}. But nothing shows up. :( Am I doing something wrong or forgetting something?
    – Kevin
    Commented Aug 22, 2016 at 15:48
  • I mistakenly thought you could add a template through the theme directory but apparently that doesn't work. You have to add it via a module. There's tutorial for adding a module here: alanstorm.com/magento_2_mvvm_mvc. Sorry for the confusion. Commented Aug 22, 2016 at 17:13

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.