How can extensions show / hide blocks conditionally in Magento 2

- Magento 2

Magento 2 Development

In Magento 2 there are several ways of showing and hiding blocks conditionally. If its visibility depends on a configuration settings value, use the ifconfig expression in your layout file:

In case of a more complex visibility condition, show and hide a block by overriding the\Magento\Framework\View\Element\AbstractBlock::toHtml() method.

The block won’t be shown If you return the empty string from the method.

The above example illustrates how the block can be hidden or shown for anonymous or authenticated visitors. The rm_customer_logged_in function detects all necessary information.

Source

More tips from the cookbook