0

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

  • What if you replace template="phtml/header.phtml" with template="Elcometer_Product::phtml/header.phtml" ? – Raphael at Digital Pianism Jun 17 '16 at 8:40
  • I tried that too. I'll try again now with grunt clean and bin/magento cache:flush – LM_Fielding Jun 17 '16 at 8:43
  • Yes, still nothing shows. I have another module that I wrote that works fine. The only difference is that module is added from another module/my theme's layout file. – LM_Fielding Jun 17 '16 at 8:46
  • any error are generate inside log file? – Rakesh Jesadiya Jun 17 '16 at 8:55
  • Merged layout errors, nothing relevant. – LM_Fielding Jun 17 '16 at 9:00
1

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
0

Your default.xml code as below:

<?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>
        <block class="Magento\Framework\View\Element\Template" name="elcoproduct" template="Elcometer_Product::phtml/header.phtml"/>
    </body>
</page>
  • I have that. It doesn't work. – LM_Fielding Jun 17 '16 at 8:51
  • Have you run command php bin/magento setup:upgrade? – Prashant Valanda Jun 17 '16 at 8:55
  • Tried that. Makes no difference. – LM_Fielding Jun 17 '16 at 9:04
  • setup:upgrade has nothing to do with missing blocks and will throw an exception if the database is not up to date with migrations anyway. – LM_Fielding Aug 11 '17 at 11:06

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.