Conditionally hide or show a block in Magento 2

Magento 2, Layout XML, Quick Tip

Somehow i only found about this little gem only recently. In your layout xml you can use the ifconfig parameter to hide or show block depending on your configuration. It works as following:

<block class="<VENDOR><MODULE>BlockMyBlockClass" ifconfig="vendor_module/group/configuration_option" name="my-block" />

Now when you make sure the config path vendor_module/group/configuration_option exists, Magento will check the value for you. If it is true-ish you block will be shown, if not it will hide the block.

MageDispatch.com (ad)

Mage Dispatch is a newsletter for the community and by the community. Here you can share links that you think that the community should know about. We will include it in our next newsletter.

Documentation

I tried to find documentation about this, but on the Magento devdocs website this keyword is not found. I have found some usages in the core though so my guess is that it's safe to use:

Want to respond?