Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using conditions in magento layout xml

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

like image 829
Jonathan Day Avatar asked Jul 26 '10 01:07

Jonathan Day


1 Answers

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.

like image 132
Alan Storm Avatar answered Sep 28 '22 09:09

Alan Storm