Hello,
Yii is awesome, easy to learn PHP framework and is brisk fast and offer so much that you can’t turn your back on it. Combined with Zend framework its a killer combination.
This post is about me showing you how to set a page title in YiiFramework.
Let check it out
Now when you install Yii and create a web project Controllers and Views directories are created by default.
Under views directory you’ve got a folder called layout
Under that folder there should be a php file with name main.php, You don’t really have to use the default template of course. You can create your own and use that making required changes to your Yii config. Anyway under main.php look for this line
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
The variable pageTitle is what we need to set. You have got 2 choices. Either set it in Controller action or In the view file it self.
For me View is somewhere I would put my pageTitle in.
Example Controller action
Say your controller file is called DefaultController.php and it has an action method Index, Below is how you set a pageTitle property.
function actionIndex(){ $this->pageTitle = "My Page Title"; }
Where $this is current instance of Controller.
Please note: If you don’t set pageTitle then this property default to Controller’ Action name
Setting up in View File
I will always set a pageTitle under a view file. I personally think that View is the right place to put your pageTitle in.
So if your View file is index.php
Then somewhere in that file do this
$this->pageTitle = "My Page Title";
Couple of points. If you are not using default Layout file then make sure that the title tag should look like this
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
You can place
$this->pageTitle = "My Page Title";
anywhere in your View file or Controller action. It is not required to be the first line or last line.
But for consistency sake you may wish to place it somewhere where other devs working on the same project can find it easily rather than to override your code.
I hope this helps you
Cheers
Advertisement





JC WordPress Coupon Revealer Plugin Pro License
Australian Street Names with City, State and Display Names only, Single Server License
Recent Comments