我正试图在local.xml文件中为我的自定义块设置一个变量:
<layout>
<!-- ... -->
<page_homepage>
<!-- ... -->
<reference name="root">
<!-- ... -->
<block type="core/template" name="home_page_sections" template="page/homepage/sections.phtml">
<block type="layout/carousel" name="featured_carousel">
<action method="setData">
<name>filter_attribute</name>
<value>is_featured_product</value>
</action>
</block>
</block>
</reference>
</page_homepage>
</layout>
但我没有在控制器的另一端获得数据:
class Foo_Layout_Block_Carousel extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$filterAttribute = $this->getFilterAttribute(); // Nothing
$filterAttribute = $this->getData('filter_attribute'); // Nada
// Alright, fine, what DO I have?!
var_dump($this->getData()); // array(0) {} ... Argh!
}
}
从我所有的搜索中,我发现这真的应该有效,但既然无效,我有一种感觉,我错过了一些显而易见的东西。以下是我的布局模块的配置(我使用单个模块来定义主页和网站所需的任何其他块):
<?xml version="1.0"?>
<config>
<modules>
<Foo_Layout>
<version>0.1.0</version>
</Foo_Layout>
</modules>
<global>
<page>
<layouts>
<foo_homepage translate="label">
<label>Homepage</label>
<template>page/homepage.phtml</template>
<layout_handle>page_homepage</layout_handle>
</foo_homepage>
</layouts>
</page>
<blocks>
<layout>
<class>Foo_Layout_Block</class>
</layout>
</blocks>
</global>
</config>
当布局呈现代码遇到此时
<block type="layout/carousel" name="featured_carousel">
它立即实例化块。这意味着块的 __construct
方法在调用您的setData
方法之前被调用。因此,在构建时,没有设置任何数据,这就是为什么对var_dump
的调用返回一个空数组的原因。
此外,在创建之后,块立即被添加到布局中
#File: app/code/core/Mage/Core/Model/Layout.php
...
$block->setLayout($this);
...
发生这种情况时,将调用块的_prepareLayout
方法。
#File: app/code/core/Mage/Core/Block/Abstract.php
public function setLayout(Mage_Core_Model_Layout $layout)
{
$this->_layout = $layout;
Mage::dispatchEvent('core_block_abstract_prepare_layout_before', array('block' => $this));
$this->_prepareLayout();
Mage::dispatchEvent('core_block_abstract_prepare_layout_after', array('block' => $this));
return $this;
}
因此,这意味着布局更新xml中的任何数据集仍然不可用,即使在_prepareLayout
中也是如此。一旦系统完成了块的创建,它就会移动到下一个XML节点。
<action method="setData">
<name>filter_attribute</name>
<value>is_featured_product</value>
</action>
并调用CCD_ 6方法。现在您的块有了它的数据集。
尝试在块上定义一个_beforeToHtml
方法,并在那里检查数据。(假设您的块正在渲染)
我认为块的定义是错误的。你能试试吗
<block type="layout/carousel"name="featured_carousel" attribute=value>
并在phtml中使用$this->getAttribute()检索值
你可以看到下一个例子:
class Elblogdeselo_Blocksparams_Block_Test extends Mage_Core_Block_Abstract{
protected function _toHtml(){
//$nombre=$this->getNombre();
$nombre=$this->getData('nombre');
$html=$html." ".$this->getData('nuevo_parametro');
return $html;
}
}
在后端的定义中,我把它放在我的家CMS 中
{{block type="blocksparams/test" name="bloque_con_parametros" nuevo_parametro="nuevo" nombre="david" template="blocksparams/test.phtml"}}
我在扩展中发现的另一个例子:
protected function _construct(){
parent::_construct();
$this->setData('customer', Mage::getSingleton('customer/session')->getCustomer());
$this->addData(Mage::getModel('model/model'));
}
将变量从布局传递到块:
<action method="setData">
<name>variablename</name>
<value>10</value>
</action>
从布局中获取块中变量的值:
$variableName = $this->getVariablename();
$variableName = $this->getData('variablename');