【问题标题】:Magento Inheriting and Re-Using Template VariablesMagento 继承和重用模板变量
【发布时间】:2010-11-09 19:02:15
【问题描述】:
是否可以将变量从父模板传递给子模板。例如,如果我想将一些重复的 HTML 放在一个单独的模板中,该模板包含在其父模板的 foreach 循环中
<?php
foreach ($items as $item)
{
echo $this->getChildHtml('item_info');
}
?>
我希望能够访问 item_info 模板中的 $item 变量。
谢谢
【问题讨论】:
标签:
xml
layout
templates
magento
【解决方案1】:
我已将我的产品列表模板拆分为一个单独的文件,以便在多个地方使用它。
在父模板中,我执行如下操作:
<?PHP
$this->getChild('product_list_list')->setData('products', $_productCollection);
echo $this->getChildHtml('product_list_list');
?>
在子模板中我可以做到:
<?php foreach ($this->products as $_product): ?>
// display products
<?php endforeach; ?>
所以你应该可以做到:
$this->getChild('item_info')->setData('item', $item);
然后在 item_info 中访问它
$this->item
希望对你有用。在 magento 1.3 上为我工作,但它似乎相当基础,因此可能对所有版本都通用。
【解决方案2】:
我知道这不是一个新帖子,但这里有一点补全:
你应该调用getChildHtml,缓存属性为false,例如:
$this->getChildHtml('item_info', false);
然后,它会完美运行。
谢谢本伦利