2

I am trying go create a custom Magento 2 theme. Within this theme I want to add a custom block to the default header.container. This is what I have so far:

app/design/frontend/Vendor/Theme/templates.header.phtml

<p>Custom Header Content</p>

app/design/frontend/Vendor/Theme/Magento_Theme/layout/default.xml

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="header.container">
            <block class="Magento\Framework\View\Element\Template" name="vendor.theme.header" template="Vendor_Theme::header.phtml" />
        </referenceContainer>
    </body>
</page>

app/design/frontend/Vendor/Theme/registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::THEME,
    'frontend/Vendor/Theme',
    __DIR__
);

However, in the front end I get the following error message:

Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'Vendor_Theme::header.phtml' in module: '' block's name: 'vendor.theme.header'

Whenever I search how to create custom blocks within Magento I always come across module implementations, i.e. within app/code/Vendor/Module/….

Since the error message says

in module: ''

I am now beginning to wonder if you aren't really supposed to define blocks within a theme at all?

1 Answer 1

4

Move the templates directory from app/design/frontend/Vendor/Theme/ to app/design/frontend/Vendor/Theme/Magento_Theme/ directory.

Then, change the template path to Magento_Theme::header.phtml in app/design/frontend/Vendor/Theme/Magento_Theme/layout/default.xml

<?xml version="1.0" ?> 
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceContainer name="header.container">
                <block class="Magento\Framework\View\Element\Template" name="vendor.theme.header" template="Magento_Theme::header.phtml" />
            </referenceContainer>
        </body> 
</page>
4
  • Ah I see. So my theme never "owns" any templates directly, they are always in the appropriate module (like Magento_Theme)?
    – fritzmg
    Feb 6, 2018 at 13:53
  • Yes. Please do upvote/accept as answer if this answers your question. It might help others too :)
    – Mike
    Feb 6, 2018 at 14:49
  • Btw. I found out that it also works if I use template="header.phtml" and put the header.phtml in the templates folder within app/design/frontend/Vendor/Theme.
    – fritzmg
    Feb 6, 2018 at 14:53
  • 1
    Great. But this approach helps us to organize the templates in the respective modules
    – Mike
    Feb 6, 2018 at 15:19

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.