Magento 2 – How to Add Text Within Container via XML

blockscontainerslayoutmagento2xml

I am attempting to render a single line of text prior to a Link list in the footer. Creating a template file to do this seems to be a waste of resources for this task. As I understood it, I could output text with a Magento\Framework\View\Element\Text block in the layout.

XML

<referenceContainer name="footer">
  <container name="footer.column.about_us" htmlTag="div" htmlClass="column about-us" before="-">
    <container name="column.about_us.label" htmlTag="div" htmlClass="label">
      <block class="Magento\Framework\View\Element\Text" name="about_us.label">
        <arguments>
          <argument name="data" xsi:type="array">
            <item name="text" xsi:type="string">About Us</item>
          </argument>
        </arguments>
      </block>
    </container>
    <block class="Magento\Framework\View\Element\Html\Links" name="footer_links.about_us">
      <arguments>
        <argument name="css_class" xsi:type="string">footer links</argument>
      </arguments>
    </block>
  </container>
</referenceContainer>

The preceding does not output anything, and I am unsure whether this is unsupported, if I'm just doing it wrong, or if there is another more proper method to accomplish this.

To be clear, what I'm currently seeing is:

<div class="column about-us">
  <ul class="footer links">...</ul>
</div>

when I want to see:

<div class="column about-us">
  <div class="label">About Us</div>
  <ul class="footer links">...</ul>
</div>

Any suggestions?

Best Answer

You need to use the argument directly without the array.

Instead of

    <arguments>
      <argument name="data" xsi:type="array">
        <item name="text" xsi:type="string">About Us</item>
      </argument>
    </arguments>

You need:

    <arguments>
      <argument translate="true" name="text" xsi:type="string">About Us</argument>
    </arguments>

Alternative

You can also try with the <action> tag:

<action method="setText">
      <argument translate="true" name="text" xsi:type="string">About Us</argument>
</action>

Adding a div directly

You can also add a div directly in the text with the following:

<argument translate="true" name="text" xsi:type="string"><![CDATA[<div class="label">About Us</div>]]></argument>
Related Topic