6

I'm trying to add a new container to a layout (extending Magento Blank) but it's not rendering in HTML. I added the following to:

app/design/frontend/VENDOR/MYTHEME/Magento_Theme/layout/default.xml

<container name="mike.container" as="mikeContainer" label="Mike Container" htmlTag="div" htmlClass="mike-container" />

I cleaned the cache and refreshed the page and then looked at the source code rendered by my browser but a div with a class of "mike-container" doesn't exist.

Do I need to do something else?

Thank you

13

David's answer is correct, but his example doesn't work (for me anyway).

If you swap the text block in his example with a template block you'll see the container then displays.

My full example to get it working:

app/design/frontend/PACKAGE/THEME/Magento_Cms/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="footer">
            <container name="mike.container" as="mikeContainer" label="Mike Container" htmlTag="div" htmlClass="mike-container">
                <block class="Magento\Framework\View\Element\Template" name="testing" template="Magento_Cms::test.phtml" />
            </container>
        </referenceContainer>
    </body>
</page>

app/design/frontend/PACKAGE/THEME/Magento_Cms/templates/test.phtml

<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatem suscipit, adipisci, cum quos, alias similique ad eum at deserunt eligendi enim dignissimos, unde vero ipsam voluptatibus cumque accusantium! Obcaecati, quasi.</h2>

Result

enter image description here

If I remove the template block, the container is no longer rendered.

  • hm ok :o maybe the arguments in the example are wrong, did not have a running env here to test it (y) – David Verholen Apr 14 '16 at 19:09
  • Guys, thank you both very much for you help! – MikeMason Apr 15 '16 at 8:53
  • Thx Ben Crook, this helps get close to making custom widget layout position – Jonathan Marzullo May 11 '18 at 15:43
6

magento2 seems to render a container only if the children generate a html output: https://github.com/magento/magento2/blob/6ea7d2d85cded3fa0fbcf4e7aa0dcd4edbf568a6/lib/internal/Magento/Framework/View/Layout.php#L571

So I think you need to have at least one childblock with an output in order to have the container rendered

you should be able to test this with a text block like this:

<container name="mike.container" as="mikeContainer" label="Mike Container" htmlTag="div" htmlClass="mike-container">
    <block class="Magento\Framework\View\Element\Text" name="testtextblock">
        <arguments>
            <argument name="data" xsi:type="array">
                <item name="text" xsi:type="string">TestText</item>
            </argument>
        </arguments>
    </block>
</container>
  • Is the Text block supposed to output the text by default? I can't get this example to work. I'm attempting to output text as a label prior to a Html/Links block. – Jonathan Eltgroth Jun 30 '16 at 15:34
  • 2
    Text block doesnt have constructor defintion so you cant pass arguments. Use action instead: <action method="setText"><argument name="text" xsi:type="string">TestText</argument></action> – nethead Nov 27 '16 at 16:52

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.