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.

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.

  • Thanks! I've added test.phtml in <theme-folder>/Magento_Theme/template. How do I get the block_id (your_block_identifier)? – Kevin Aug 21 '16 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. – Aaron Allen Aug 21 '16 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 Aug 22 '16 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. – Aaron Allen Aug 22 '16 at 17:13

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.