Dynamic pages titles

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



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">


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


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?

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.

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

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

good topic! thank you :lol:

Thanks, it’s what I need!!

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.

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;


        return true;


this helped me, thank you.

Thanks, it help me as well. :)

thank you, great!

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