I’m note sure I understand what you specific needs are and I don’t know how advanced you are with Yii stuff. But If you only want to display additionnal messages or modules, you don’t need to change of layout.
2 basic things which are helpful:
With the function render (which is pretty much the first thing called), you can pass parameters. For a same layout, you can use multiple views + parameters. In my applications, the layout is just the main frame and is almost php free. But it’s up to you where you put the line between content and layout.
You can create and use widgets (and pass them parameters): useful for displaying a similar or identical content on different pages (DRY principle).
You can’t access those params with your layout php file. Maybe you need to transfer some of your layout stuff inside your view pages? Or use a different layout? It really depends what you are trying to do.
Think simple and what’s the best way to get a easy-to-maintain, easy-to-modify & re-usable code.
You can access Controller’s properties inside a layout. So if you need to use some variables in a layout (e.g. $userMessage), you have to define a property “userMessage” in a controller and access it in a layout this way:
Or you can use widgets. It all depends on the nature of the message (if it’s for guests only, if you get it from your db, if it is user-personalized…).
Maybe dotangelo if you explain a bit more, we’ll find out what is the best way.
I just want to display kind of "welcome message" for every user on the main (index) page.
I tought it would be as easy as : if(controller == index and action == index) display_message()
But firstly I’m not sure if this is a good practice, and secondly, I don’t think that defining properties (as andy_s states) for that really simple purpose is a good idea.
Oh, and sorry for my english, it’s not my native language :/.
As view parameters can differ between views, they are only available inside the rendered view, not the layout. E.g. what if you check for a variable $welcomeMessage in your layout, but only set that in a “index” view, not a “list” view? In strict mode you’d get an error message.
Another approach would be like and_s suggested: You could add a public variable to your basecontroller (in protected/components/Controller.php) like $showWelcome=false; to make it available in all views. Then you set that variable from your controller action whenever you want the welcome message to be displayed.