Question

I need to set some tags(title, description) after i render a layout. I know how to set the titles via layout files:

<reference name="head">
 <action method="setTitle" translate="title"><title>Your Title</title></action> 
</reference>

or via code by accessing head block element and calling setTitle('your title') method on it

$this->getLayout()->getBlock('head')->setTitle('your title'); 

but i need the titles to be dynamically fetched from an item from a collection.

If i go with the controller approach i will be loading the same element twice since i need to load the product to get the fields to set them as Title and Description and again in the block to show it in the product page.

Is it wise to save it with Mage::register('variable', 'value');? Should i create the entire layout programatically and assign the data with $this->getLayout()->getBlock('your.block.name.in.the.layout')->assign('data', $data);?

Was it helpful?

Solution

When you are talking about a product, you should try to get the product before the renderLayout call, to access the head block and get the product from Mage::registry('product') or Mage::registry('current_product')

OTHER TIPS

You can assign a new block without template in the content, that is loaded after the collection. In this block get all necessary information by getting data from main block (like product) or registry. Then call getBlock('head')->setTitle('your title'); from _prepareLayout

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top