views:

178

answers:

1

Hi all,

Wondering if anyone has used the or statements in magento's layout XML for a custom module? I realise that I could check the values in the module controller or block itself, but it seems like a logical place for the logic to live.

Mage_Core uses them in catalog.xml to test for javascript. <!--<params/><if/><condition>can_load_calendar_js</condition>-->

Thanks, JD

+3  A: 

I'd shy away from using those. You'll notice they're commented out in the default distributed community edition, which probably means the core team is moving away from using them.

If you're interested in what they do, they're paramaters that are strictly a part of the page/head block's addItem method.

File: code/core/Mage/Page/Block/Html/Head.php
public function addItem($type, $name, $params=null, $if=null, $cond=null)
{
    if ($type==='skin_css' && empty($params)) {
        $params = 'media="all"';
    }
    $this->_data['items'][$type.'/'.$name] = array(
        'type'   => $type,
        'name'   => $name,
        'params' => $params,
        'if'     => $if,
        'cond'   => $cond,
   );
    return $this;
}

The add item method stores these conditions, and then they're used later in the getCssJsHtml method to skip adding an item.

public function getCssJsHtml()
{
    // separate items by types
    $lines  = array();
    foreach ($this->_data['items'] as $item) {
        if (!is_null($item['cond']) && !$this->getData($item['cond']) || !isset($item['name'])) {
            continue;
        }

My guess is they were an early attempt to add meta-programming to the template system, which ended up going over the head of its intended users.

Alan Storm
Thanks Alan, comprehensive and information answer as always. I guess I'll stick to using the Block for the conditional logic.JD
Jonathan Day