Magento2 – Custom Block Template File Not Showing

magento2

Edit: Slightly better understood

If I attempt to load my block from inside the module's default.xml it doesn't work, if I load it from Magento_Theme's default.xml layout file then it appears on the page?


Edit: I have it working but don't know why.

It depends on the container. If I reference a container and then add my block inside that it displays.

If I place my block just in it does not display.


All of the following paths are in the app/code directory.

I simply want to display my template file on the page, and it isn't showing. The module is enabled, cache cleared, grunt clean… still nothing.

Template: Website/Product/view/frontend/templates/phtml/header.phtml
Template file content:

    <div>
         <p>Testing, testing, testing</p>
    </div>

Layout file: Website/Product/view/frontend/layout/default.xml
Layout file content:

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

Website/Product/etc/module.xml
Content:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Website_Product" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Website/Product/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Website_Product',
    __DIR__
);

Things I've tried:

replacing template="phtml/header.phtml" with template="Website_Product::phtml/header.phtml" in layout.xml

Best Answer

When trying to use the layouts that have been previously created by Magento, you have to first decide if you are trying to edit the layout of a page, or the content within it.

For example, within the Magento/Theme/view/frontend/layout/default.xml file, you will see code such as this:

<referenceContainer name="main">
            <container name="content.top" label="Main Content Top"/>
            <container name="content" label="Main Content Area"/>
            <container name="content.aside" label="Main Content Aside"/>
            <container name="content.bottom" label="Main Content Bottom"/>
</referenceContainer>

The containers allow you to add content within one of these area's within the page. So for example, if we wanted to add our own custom content to the "content" block (often used in Magento1) we could do so by doing something like the following in our own layout file:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-right" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Namespace\Module\Block\BlockName" name="custom_name" template="Namespace_Module::template.phtml"/>
        </referenceContainer>
    </body>
</page>

The reason as to why it may not have been showing previously, could be because of one of the following:

  1. A container for your block to be shown
  2. An incorrect path to the template being used
  3. An incorrect handle for the file name (not in your case but might be useful for others) i.e. catalog_product_view.xml could be used to add content to a product page
Related Topic