【问题标题】:Magento different examples for inserting an existing blockMagento 插入现有块的不同示例
【发布时间】:2014-11-18 15:59:24
【问题描述】:

我想了解我在不同资源中找到的示例代码的区别,以通过 Magento local.xml 文件插入块。

<action method="insert"><name>catalog.compare.sidebar</name></action>

<action method="insert"><block>catalog.compare.sidebar</block></action>

<action method="insert"><blockName>catalog.compare.sidebar</blockName></action>

每种方法的动作方法相同,但参数名称不同。 每个示例似乎都可以正常工作,那么在这种情况下使用“name”、“block”或“blockName”有什么区别?

提前致谢。

【问题讨论】:

    标签: xml magento block


    【解决方案1】:

    通过这种方式,您基本上告诉 Magento 调用由您的操作节点的父节点指定的块的方法“插入”。 (或在 app/code/core/Mage/Core/Block/Abstract.php 中)

    Magento 不关心子节点的名称,只关心它们的值。

    你可以在app/code/core/Mage/Core/Model/Layout.php中观看

    public function generateBlocks($parent=null)
    {
            if (empty($parent)) {
                $parent = $this->getNode();
            }
            foreach ($parent as $node) {
                $attributes = $node->attributes();
                if ((bool)$attributes->ignore) {
                    continue;
                }
                switch ($node->getName()) {
                    case 'block':
                        $this->_generateBlock($node, $parent);
                        $this->generateBlocks($node);
                        break;
    
                    case 'reference':
                        $this->generateBlocks($node);
                        break;
    
                    case 'action':
                        // WE GO HERE WHEN WE HAVE AN ACTION NODE
                        $this->_generateAction($node, $parent); 
                        break;
                }
            }
    }
    

    那你看_generateAction()的时候,我就不贴所有方法了:

    .... 
    
    $args = (array)$node->children();
    
    ....
    
    call_user_func_array(array($block, $method), $args);
    

    所以,无论参数 tagName 是什么,结果都是一样的。

    干杯,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多