是否可以将变量从父模板传递到子模板。例如,如果我想将一些重复的 HTML 放入一个单独的模板中,该模板包含在其父模板的 foreach 循环中
<?php
foreach ($items as $item)
{
echo $this->getChildHtml('item_info');
}
?>
我希望能够访问 item_info 模板中的 $item 变量。
谢谢
最佳答案
我已将产品列表模板拆分为一个单独的文件,以便我可以在多个地方使用它。
在父模板中,我做了如下操作:
<?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 上工作,但它似乎相当基础,所以可能对所有版本都是通用的。
关于xml - Magento 继承和重用模板变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1110770/