This is a no-brainer module, but still extremly useful and one of the features where I'm thinking: Why didn't Magento implemented this into the core in first place? Aren't others missing this feature?

And as I'm going to use this over and over again across all future projects, I've put this feature into an own module:

And here it is: The "ifconfig" attribute for the <block> and the <reference> tag in layout xml! (<action method="..."> had it before).

The ifconfig attribute allows you to specify a path to Magento's configuration (e.g. dev/log/active) that will be evaluated as a boolean value (using Mage::getStoreConfigFlag()). If this configuration option is set to true the action method will be called, the block will be created or the content of the reference will be evaluated.

Custom configuration options can easily be added inside own Magento modules (defined in system.xml and defaults set in config.xml).

This enabled you to create blocks that can be easily enabled or disabled from the configuration without touching any layout updates.

Please note that adding a condition to a reference block will only apply to the layout updates inside the reference and will NOT add the condition to the block you're referencing to. So it is not possible to add a condition to a block somewhere else than to the place where the actual block is specified.

Example

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="content">
            <block type="core/text" name="say.hello" ifconfig="mysettings/foo/sayhello">
                <action method="setText"><text>Hello, World!</text></action>
            </block>
        </reference>
    </default>
</default>

And if you have a closer look at the Magento files (in app/design/frontend/base/default/layout/rss.xml) you'll see that they're already using this feature without having it implemented :)

Aoe_LayoutConditions

Get my Magento module Aoe_LayoutConditions from GitHub.

Or download the latest version directly from there:

Aoe_LayoutConditions

[Update] Version 0.2.0

A new attribute "unlessconfig" is evaluated now and will negate the configured configuration flag. The attribute can be used in blocks, actions and references. (Thanks, Colin for the suggestion...)

<reference name="content">
    <block type="core/text" name="test" before="-" unlessconfig="dev/log/active">
        <action method="setText"><text><![CDATA[<div>Logging disabled</div>]]></text></action>
    </block>
    <block type="core/text" name="test" before="-" ifconfig="dev/log/active">
        <action method="setText"><text><![CDATA[<div>Logging enabled</div>]]></text></action>
    </block>
</reference>

Comments

This website uses disqus for the commenting functionality. In order to protect your privacy comments are disabled by default.

Enable Comments