Yii Framework Forum: Dynamic pages titles - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dynamic pages titles A simple way to do this (I am not sure it's the right way)

#1 User is offline   Luciano 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 143
  • Joined: 20-September 09
  • Location:Buenos Aires, Argentina

Posted 22 October 2009 - 10:33 AM

Disclaimer: sorry if this is too basic... :P

Well...the thing is I needed a way to show several page titles (the HTML title in the HEAD).
I am not sure if is there a better way but I did this:

In the controller/action I want to show a diferent title:

public function actionIndex()
{
	$this->pageTitle = "Whatever"; // It could be something from DB or...whatever
	$this->render('index');
}



In the Layout (the magic happens in the TITLE tag):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="language" content="en" />
	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/styles.css" />
	<title><?php echo isset($this->pageTitle) ? $this->pageTitle : Yii::app()->name; ?></title>
</head>


Whenever you don't specify a $pageTitle var in the action, it shows the application name from the config file.
Is there a "better" solution?
Company web site: pressEnter

Free as in "beer"...
1

#2 User is offline   guillemc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 14-October 09

Posted 22 October 2009 - 11:16 AM

Be aware that in controllers $this->pageTitle is always set (defaults to the controller name and the action name).

Personally I love that in Yii you can set $this->pageTitle directly in the views, since that's the more logical place to have it, in my opinion.
0

#3 User is offline   alexFidelis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 17-November 11

Posted 19 November 2011 - 06:10 PM

*
POPULAR

In the VIEW page (index.php, view.php, create.php etc)

$this->setPageTitle('custom page title');

6

#4 User is offline   Nur Rochim 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 04-February 13
  • Location:Indonesia

Posted 22 May 2013 - 01:14 AM

good topic! thank you :lol:
0

#5 User is offline   jacktric 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 25-October 12

Posted 05 July 2013 - 05:28 AM

View PostalexFidelis, on 19 November 2011 - 06:10 PM, said:

In the VIEW page (index.php, view.php, create.php etc)

$this->setPageTitle('custom page title');



Thanks, it's what I need!!
1

#6 User is offline   Cozumel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 08-October 12

Posted 15 August 2013 - 10:56 AM

Old topic but DON'T add that to your view file, add it in the controller action that renders the view. Follow basic MVC principles.
1

#7 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 15 August 2013 - 01:51 PM

Or add this in the beforeAction() method, possibly transforming the action id into the title like so:
    protected function beforeAction($action) {
        if (!parent::beforeAction($action))
            return false;
        $this->setPageTitle(ucfirst($action->getId()));

        return true;
    }

Don't be a dick.
1

#8 User is offline   Neovash 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 09-November 13
  • Location:Philippines

Posted 09 November 2013 - 02:31 AM

this helped me, thank you.
0

#9 User is offline   bandpay 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 20-June 13
  • Location:Denmark

Posted 11 November 2013 - 10:38 AM

Thanks, it help me as well. :)

/*
May
The Life
Be Good To You
*******************
*******************
SincerelYii
Babak Bandpey
*/

0

#10 User is offline   hajdar 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 16-November 13

Posted 17 November 2013 - 03:28 PM

thank you, great!
0

#11 User is offline   Emma89 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 22-December 13

Posted 23 December 2013 - 08:54 PM

Thanks u! great topic! Merry Christmas and Happy New Year
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users