I would like to render a widget in my template by its ID, just like I have with blocks:
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.Sign up to join this community
With the way your doing it, you are hard coding the actual widget id. That would be fine but a better way is for you to be able to use a container where you can change the widget id through the admin, letting you easily change the widget any time you like. This can be done by creating a container on your default.xml which you can override through your theme on Magento_Theme/layout/default.xml see example xml for adding the custom container.
<?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="main"> <container name="example.block" label="Example Block" htmlTag="div" htmlClass="example-block" before="-" /> </referenceContainer> </body> </page>
This will add a container which you can then use on the widget creation. In my example i used the main container as a reference but you can use any reference you would like depending on your needs.