How to conditionally add a block (depending on the configuration in the admin panel) in Magento's layout XML?
We can check if config is true on actions. In the example below, if sample/config/show_toplinks
config from the admin panel (in System->Configuration) is true, then the template file links.phtml
will be used to render Top Links. If sample/config/show_toplinks
is false, then the default template will be used.
<reference name="top.links">
<action method="setTemplate" ifconfig="sample/config/show_toplinks">
<template>page/template/links.phtml</template>
</action>
</reference>
I found this workaround somewhere in the web. We can set an empty template as a default template for Top Links, like this:
<reference name="top.links">
<action method="setTemplate" ifconfig="sample/config/show_toplinks">
<template>page/template/links.phtml</template>
</action>
<!-- OR set completely empty template -->
<action method="setTemplate">
<template>page/template/empty_template_for_links.phtml</template>
</action>
</reference>
In this case, if sample/config/show_toplinks
is true, then the template links.phtml
will be used and Top Links will be displayed. but if sample/config/show_toplinks
is false, then the empty_template_for_links.phtml
template will be used and that template is completely empty, so it doesn't return any HTML and the Top Links will not be visible.
- Is there any other way to conditionally show or hide blocks depending on the configuration in the admin panel?
- Is this workaround safe?
- Can this cause any unexpected errors?