Reputation: 1651
Is it possible to pass a variable from a parent template to a child template. For example, if i wanted to put some repeated HTML in a separate template that was included within a foreach loop in its parent template
<?php
foreach ($items as $item)
{
echo $this->getChildHtml('item_info');
}
?>
I would want to be able to access the $item variable within the item_info template.
Thanks
Upvotes: 2
Views: 3198
Reputation:
I know it's not a new post, but here is a little completion :
you should call getChildHtml with cache attribute at false, such as :
$this->getChildHtml('item_info', false);
And then, it will work perfectly.
Thank you benlumley
Upvotes: 1
Reputation: 11382
I've split my product list template to a separate file so I can use it in several places.
In the parent template, I do something like:
<?PHP
$this->getChild('product_list_list')->setData('products', $_productCollection);
echo $this->getChildHtml('product_list_list');
?>
In the child template I can do:
<?php foreach ($this->products as $_product): ?>
// display products
<?php endforeach; ?>
So you should be able to do:
$this->getChild('item_info')->setData('item', $item);
and then within item_info, access it as
$this->item
Hope that works for you. Works for me on magento 1.3, but it seems fairly fundamental, so probably common to all versions.
Upvotes: 4