magento 2 custom phtml page

5k Views Asked by At

Having just got myself acquainted (enough) with Magento 1.9, and able to make the customisations required, I've been told that once it's out, we're moving to Magento v2.0. Having found the differences in the file structure, I believe I can see where to place my code for the custom pages we use, but how can I add this page from my project to a static block for later use? Previously, the method used was as follows (I realise this may not follow the best practise, but it worked):

  1. Create element folder within theme or core templates directory. eg. /app/design/frontend/<theme>/default/template/myelement/mypage.phtml
  2. Add this page/element to a static block using the following in the content editor:

    {{block type="<theme>/default" template="myelement/mypage.phtml"}}

This block can then be added to the category pages as required.

In Magento 2, I have tried what I believe to be required, which is replicating the file structure and adding phtml files to this, so the template file now resides in:

/app/code/<supplier>/<module>/view/frontend/templates/mypage.phtml

Adding to the content editor the following:

{{block type="<supplier>/<module>" template="mypage.phtml"}}

Unfortunately, this does not display the intended page (element). It doesn't display the calling "{{block" entry either, which usually happens when the line is invalid, so I can only assume that I'm missing something with the link to this template.

If anyone can offer some assistance with this, I would be most grateful.

EDIT: Continued research on this has led me to the following assumptions:

  1. Magento 2 requires more than just a new .phtml page, even for simple customizations.
  2. I'm still missing something.....

Having gone through 3 different tutorials on creating new modules for Magento 2, each providing slightly different methods, but fundamentally being the same thing, I now have what I believe should be all the code elements to make a new .phtml template for display in a static block.

This has led to an additional problem though. While I have the required code, I cannot add the module. Adding the module to etc/config.php, as suggested in 2 out of the 3 tutorials, simply crashed M2, both admin and frontend when you try to clear the cache. This is the case after manually clearing cache folders in the var directory.

Also, still unable to add the .phtml template file to a static block or page using content editor.

Not much hair left to pull out here, so looking for help! Thanks in advance

3

There are 3 best solutions below

2
On

You Should try "class" instead of "type". So your code should look like.

{{block class="<package>\<module>\Block\MyBlock" template="mypage.phtml"}}
1
On

There are a few samples on GitHub, including https://github.com/magento/magento2-samples/tree/master/sample-module-newpage which shows a module adding a new page with a very simple PHTML template file. This example does not use CMS content editing however - it is a sample based on using layout files.

You mentioned you were getting crashes. Would need more details to help on that one. If you got it solved, could you update this question and accept a response to close it out? Thx!

0
On

You Should try "VenderName_Modulename::myelement/mypage.phtml" instead of "myelement/mypage.phtml". So your code should look like.

 {{block class="VenderNameModulename\Block\MyBlock" template="VenderName_Modulename::myelement/mypage.phtml"}}