How to add widget to layout in Magento 2?

V.S picture V.S · Jul 22, 2016 · Viewed 10.1k times · Source

Is there a way to add widget to the layout xml? I know about the layout updates from the backend, but I wanted to know if there is a way to add widgets the same way as static blocks?

Answer

Emizen Tech picture Emizen Tech · Jul 25, 2016

you can add widget via followings xml ,

<referenceContainer name="content.top">
    <block class="Magento\Catalog\Block\Category\Widget\Link" name="demoBlock" template="widget/static_block/default.phtml">
        <action method="setData">
            <argument name="id_path" xsi:type="string">category/20</argument>
            <argument name="title" xsi:type="string">Demo Category Title</argument>
            <argument name="template" xsi:type="string">category/widget/link/link_inline.phtml</argument>
        </action>
    </block>
</referenceContainer>

you need to update followings

  • referenceContainer as per you needs
  • Block Class & Template (if you require to update the template for block)
  • arguments in setData