Dynamic Page Title on Custom Module in Magento 1

magento-1module

Where and how to set titles in custom module?

I got a custom (but close to standard) news module with router,controller,etc … and want to set title for single news view dynamically.

Please let me know if you need any files.

Best Answer

You can the page title using ->setTitle();

For example my page title is My title; the add below code in between $this->loadLayout(); and $this->renderLayout(); in controller action, and just change title according action or it action parameters I,e

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

Add the code in Controller

$this->loadLayout();
...
$this->getLayout()->getBlock('head')->setTitle($this->__('My title'));
$this->renderLayout();

Example: same action but different parrmeters

custommodule/controller/myindex/id/1(for this url i want set title Amit)
custommodule/controller/myindex/id/5 (for this url i want set title Bera)

then in MymoduleContoller.php at action myindexAction code is like

 $this->loadLayout();
   ...
if($this->getRequest()->getParam('id')=5){
    $this->getLayout()->getBlock('head')->setTitle($this->__('Bera'));

}elseif($this->getRequest()->getParam('id')=1){
  $this->getLayout()->getBlock('head')->setTitle($this->__('Amit'));
}
else{
  $this->getLayout()->getBlock('head')->setTitle($this->__('My title'));
}

$this->renderLayout();

If want using xml then try below

<layout version="0.1.0">
    <custommodule_mymodule_myindex>
        <reference name="head">
            <action method="setTitle"><title>My Module Page</title></action>
        </reference>

    </custommodule_controller_myindex>
</layout>

http://blog.chapagain.com.np/magento-setchange-page-layout-title-tag-meta-keywords-and-description/ http://inchoo.net/ecommerce/magento/change-any-page-title-in-magento/

Related Topic