1

I'm following an exercise in the ebook 'No-Frills-Magento-2-Layout' by Alan Storm (excellent book so far btw) and am coming across a slight issue for which I am not sure what I am doing wrong.

I am trying to create a new theme with the intention of adding a new block to the catalog_category_view layout template.

I have the following code structure:

/design
    /frontend
        /Pulsestorm
            /dram
                /Magento_Catalog
                    registration.php
                    theme.xml
                    /layout
                        catalog_category_view.xml
                    /templates
                        hello.phtml

The files contents are as follows:

registration.php:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::THEME,
    'frontend/Pulsestorm/dram',
    __DIR__
);

theme.xml:

<?xml version="1.0"?>
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
    <title>Pulsestorm Dram</title>
    <parent>Magento/luma</parent>
    <media>
        <preview_image/>
    </media>
</theme>

catalog_category_view.xml:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block name="pulsestorm_magento_catalog_category_view_example" class="Magento\Framework\View\Element\Template" template="hello.phtml"/>
        </referenceContainer>
    </body>
</page>

hello.phtml:

<h2>Hello Category Listing Page</h2>

I then registered the theme in admin in Content -> Design -> Themes for each website listed (the default websites that ship with Magento 2).

I then cleared the cache: magento cache:clean

and then navigated to: [development-web-address]/gear/fitness-equipment.html

but I then get the following exception:

1 exception(s):
Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'hello.phtml' in module: '' block's name: 'pulsestorm_magento_catalog_category_view_example'

Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'hello.phtml' in module: '' block's name: 'pulsestorm_magento_catalog_category_view_example'
#0 /srv/magento/vendor/magento/framework/View/Element/Template.php(300): Magento\Framework\View\Element\Template->fetchView(false)
#1 /srv/magento/vendor/magento/framework/View/Element/AbstractBlock.php(667): Magento\Framework\View\Element\Template->_toHtml()
#2 /srv/magento/vendor/magento/framework/View/Layout.php(557): Magento\Framework\View\Element\AbstractBlock->toHtml()
#3 /srv/magento/vendor/magento/framework/View/Layout.php(533): Magento\Framework\View\Layout->_renderBlock('pulsestorm_mage...')
...

I am expecting <h2>Hello Category Listing Page</h2> to be displayed after the product listings. Why am I getting this exception? From the exception message, I'm guessing that it may have something to do with the blank module name but I'm not sure why it is blank.

1 Answer 1

2

1- Your theme architecture should be like this:

--app/design/frontend/Pulsestorm/dram/
                                     |__ Magento_Catalog/
                                                        |__ layout
                                                        |__ templates
                                     |__ registration.php
                                     |__ theme.xml

2- So the registration.php theme.xml must be inside : app/design/frontend/Pulsestorm/dram/ and not inside Magento_Catalog.

3- app/design/frontend/Pulsestorm/dram/Magento_Catalog/layout/catalog_category_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="pulsestorm_magento_catalog_category_view_example"  template="Magento_Catalog::hello.phtml"/>
        </referenceContainer>
    </body>
</page>

3- app/design/frontend/Pulsestorm/dram/Magento_Catalog/templates/hello.phtml

<h2>Yehoo, it works !<h2>

If you didn't notice it , I add Magento_Catalog:: in xml part template="Magento_Catalog::hello.phtml"/> and why ?

Explanation:

When you declared you phtml in xml, you tel to Magento, please Magento render my hello.phtml, ok so Magento go to get it from your theme Magento_Theme => app/design/frontend/Pulsestorm/dram/Magento_Theme and it didn't find it ! but now by adding Magento_Catalog, Magento know now that you want to get your file from Magento_Catalog, so it goes to get it from there.

Good luck.

2
  • It seems that I'd made a few mistakes here. Thank you for your answer and for the useful explanation. Very helpful.
    – Dangerous
    Nov 10, 2018 at 11:02
  • You're welcome :) good luck
    – PЯINCƎ
    Nov 10, 2018 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.