Yii framework custom headers
I want different headers for my landing page and all other pages in my webapp. What is the best way to do this?
AS far as my understanding goes, the header and footer are loaded from the view/layouts/main.php, In my case since I am using a theme, it is loaded from themes/new/views/layouts/main.php
Now I want the header section for all my pages except the landing page, to use the header section as defined in the "main.php", however the landing page should have a different header. How should I do this? Should I use a render partial? And since I am Yii newbie, how should I do this?
Can I also use one of the other layouts files, column1.php or column2.php? And how?
开发者_Python百科I am not looking for extensive hand holding here. I just need a heads up, as to how people have implemented similar functionality.
It partly depends on how different the headers are going to be in terms of your approach. If you want them to be completely separate, you could use additional layout files either in combination with main.php or instead of it.
You set the layout file at the start of the controller class with something like:
public $layout='//layouts/column2';
This will set the default layout for a controller. You can change the value in an action function or evaluate a condition in the "beforeAction" function.
The default generated admin pages (with "gii" or the command-line) utilize those column1.php and column2.php layouts with main.php and provide a decent example to see how they work. Just move the content out of main.php you want to customize and put it into separate layout files. If you still are sharing content, you can leave the shared content in main.php.
If it's just a matter of changing a few attributes, you can use $this->getAction()->getId()
to get the action name and use that to change what content is loaded within the layout, e.g., a certain css or js file. Any complex logic you would want to do in the controller.
For something like a nav bar or similar, you could also use include
or renderPartial
base on an environment variable you set in the controller.
I'm usually create a widget like HeaderWidget with few view files and include in main layout. In controller or action define necessary view file of header and pass them into widget.
In base controller you can define property public $headerName = 'defaultHeaderView' And set value depends of some conditions.
Of cource, you need to create BController extends CController and all other controllers extends from your BController
精彩评论