4

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.

4 Answers 4

15

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/

3
  • sry but i dont get it. how is that "My Module Page" dynamic? Commented Jun 13, 2014 at 9:40
  • got it now! good edit :) did that to set title: $urlKey = trim($this->getRequest()->getPathInfo(), '/'); $parts = explode('/', $urlKey); $title = urldecode($parts[1]); Commented Jun 13, 2014 at 10:14
  • Please make sure to use a double = for comparisons. if($this->getRequest()->getParam('id') == 5){
    – Niels
    Commented Jun 6, 2017 at 14:13
1

Set Title and Meta Data in Custom Module

public function indexAction()
{
    $this->loadlayout();
    $this->getLayout()->getBlock('head')
                           ->setTitle($this->__('My title'))
                           ->setKeywords("My, Custom, Module, Keywords")
                           ->setDescription("My, Custom Module Description");
    $this->renderlayout();
}
0

Magento controller title goes as like:

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

In page/html/head.phtml template replace default

1 < ?php echo $this->getTitle() ?> with

< ?php echo ($this->getForcedTitle()) ? Mage::getStoreConfig('design/head/title_prefix').' '.$this->getForcedTitle().' '.Mage::getStoreConfig('design/head/title_suffix') : $this->getTitle() ?>

or if you don’t wish to use title prefix/sufix added from Magento admin, simply with

< ?php echo ($this->getForcedTitle()) ? $this->getForcedTitle() : $this->getTitle() ?>

and now just set “forced” title for pages through layout files

<reference name="head">
<action method="setForcedTitle"><title>Account Dashboard</title></action>
</reference>
Example for My Account page in layout/customer.xml:
<customer_account>
<reference name="head">
<action method=”setForcedTitle”><title>Account Dashboard</title></action>
</reference>
…
</customer_account>
0

By this approach you make a dynamic formation page of custom module

You should redirect the the request to one of your controller actions. From there you can use the default Magento layout/block system to present the data you want the customer to see.
Here is what worked for me.
app/etc/modules/Easylife_Brand.xml - declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Brand>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Core />
            </depends>
        </Easylife_Brand>
    </modules>
</config>

app/code/local/Easylife/Brand/etc/config.xml - configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Brand>
            <version>0.0.1</version>
        </Easylife_Brand>
    </modules>
    <frontend>
        <routers>
            <brand>
                <use>standard</use>
                <args>
                    <module>Easylife_Brand</module>
                    <frontName>brand</frontName>
                </args>
            </brand>
        </routers>
        <layout>
            <updates>
                <easylife_brand>
                    <file>easylife_brand.xml</file>
                </easylife_brand>
            </updates>
        </layout>
    </frontend>
    <global>
        <events>
            <controller_front_init_routers>
                <observers>
                    <brand>
                        <class>Easylife_Brand_Controller_Router</class>
                        <method>initControllerRouters</method>
                    </brand>
                </observers>
            </controller_front_init_routers>
        </events>
    </global>
</config>

app/code/local/Easylife/Brand/Controller/Router.php - custom router.

<?php
class Easylife_Brand_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract{
    public function initControllerRouters($observer){
        $front = $observer->getEvent()->getFront();
        $front->addRouter('brand', $this);
        return $this;
    }
    public function match(Zend_Controller_Request_Http $request){
        if (!Mage::isInstalled()) {
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect(Mage::getUrl('install'))
                ->sendResponse();
            exit;
        }
        $pathInfo = trim($request->getPathInfo(), '/');
        $params = explode('/', $pathInfo);
        if(isset($params[0]) && $params[0] == 'brands') {
             //redirect to mymodule/brands/index/brand_name/addidas
             $request->setModuleName('brand')  
                     ->setControllerName('index')
                     ->setActionName('index');
            if (isset($params[1])){
                 $request->setParam('brand_name', $params[1]);
            }
            $request->setAlias(
                Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                $pathInfo
            );
            return true;
        }
        return false;
    }
}

app/code/local/Easylife/Brand/controllers/IndexController.php - the controller

<?php
class Easylife_Brand_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        $brandName = $this->getRequest()->getParam('brand_name', null);
        $this->loadLayout();
        $block = $this->getLayout()->getBlock('brands');//get the block instance
        if ($block){ //if block is valid
             if (is_null($brandName)){//if no brand name is supplied
                 $block->setTemplate('easylife_brand/no_brand.phtml');
             }
             else{
                 $block->setTemplate('easylife_brand/adidas.phtml');
             }
        }
        $this->renderLayout();
    }
}

app/design/frontend/base/default/layout/easylife_brand.xml - layout file

<?xml version="1.0"?>
<layout>
    <brand_index_index>
        <reference name="content">
            <block type="core/template" name="brands" as="brands" />
        </reference>
    </brand_index_index>
</layout>

app/design/frontend/base/default/template/easylife_brand/no_brand.html - demo template shown when no brand parameter is passes.

<div>
No brand selected:<br /><br />
List of brands:<br />
Adidas<br />
Nike<br />
</div>

app/design/frontend/base/default/template/easylife_brand/adidas.html - demo template shown when a brand parameter is passes.

<div>This is the adidas brand page.</div>

Clear the cache.
Now when I call mysite.com/brands/ I get the first template (the one with 'No brand selected'.
If I call mysite.com/brands/addidas or mysite.com/brands/something I get the second template. ('this is the adidas brand page').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.