Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sometimes you just need an empty container in Magento 2. But when a container is empty (contains no blocks) it is not rendered. This way you can add a html comment inside the container.
<block class="Magento\Framework\View\Element\Text" name="comment.placeholder">
<arguments>
<argument name="text" xsi:type="string"><![CDATA[<!-- -->]]></argument>
</arguments>
</block>
@chedaroo

This comment has been minimized.

Copy link

@chedaroo chedaroo commented Feb 2, 2017

Thanks this helped me. Just a quick note, that this can be trimmed down further if you just need the container to render and nothing else. A self closing, anonymous block without arguments seems to work just fine, at least in 2.1.3. Plus no random comments in markup.

<container name="some.container" htmlTag="div" htmlClass="container">
    <!-- Force container to render -->
    <block class="Magento\Framework\View\Element\Text" />
</container>
@CNSam

This comment has been minimized.

Copy link

@CNSam CNSam commented Jul 7, 2017

@chedaroo seems this doesn't work in 2.1.7

@quisse

This comment has been minimized.

Copy link

@quisse quisse commented Aug 24, 2017

@CNSam because of this @chedaroo's solution doesn't work anymore. When in need an empty container, the shortest solution I've found atm is following (open for suggestions)

<!-- Force container to render -->
<block class="Magento\Framework\View\Element\Text">
    <arguments>
        <argument name="text" xsi:type="string"><![CDATA[&nbsp;]]></argument>
    </arguments>
</block>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment