Magento继承和重用模板变量

时间:2009-07-10 17:08:26

标签: xml layout templates magento

是否可以将变量从父模板传递到子模板。例如,如果我想将一些重复的HTML放在一个单独的模板中,该模板包含在其父模板的foreach循环中

<?php

foreach ($items as $item)
{
    echo $this->getChildHtml('item_info');
}

?>

我希望能够访问item_info模板中的$ item变量。

由于

2 个答案:

答案 0 :(得分:4)

我已将产品列表模板拆分为单独的文件,因此我可以在多个地方使用它。

在父模板中,我执行以下操作:

<?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上为我工作,但它似乎相当基础,所以可能在所有版本中都很常见。

答案 1 :(得分:1)

我知道这不是一个新帖子,但这里有一点完成:

你应该使用cache属性调用getChildHtml,例如:

$this->getChildHtml('item_info', false);

然后,它会完美运作。

谢谢benlumley