Magento 1 – Set Page Title After loadLayout and renderLayout

modulepage-titleseo

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);?

Best Answer

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')

Related Topic