I guess you learned the second approach from the hangman demo. Unfortunately, the hangman demo is not a very good example for learning view rendering. It is mainly meant to show the viewstate feature of Yii (which may not be used at all in an application). The blog demo is much better example.
Yes, it is right there are two ways to pass data to a view: push and pull. Your first approach uses the push method, while the second approach the pull method. In your second approach, it's more common to use a private member to store the message instead of pageState because pageState is mainly used by viewState and requires you to use CHtml::statefulForm().
The advantage of the second approach is that you don't need to generate the data unless you use it in the view. This is especially useful when your view is caching a few fragments. For example, if your view lists a few posts. You could enclose it in a fragment cache using COutputCache so that the post data doesn't need to be fetched from database for every request. However, if you are using the first approach to pass the post data, the fragment cache won't help you.
I’m just starting out with Yii after doing some reading about it but am finding it quite difficult to get my head around Yii. I’m having a bit of an issue with passing data into the view. I don’t really like the push way as it is quite cumbersome. I want to do the pull way but it looks like I have to declare a class variable everytime I want to do that. Am I missing something? Isn’t there an easy way to make data available to the view without having to put them all into a single array or without having to declare class variables everytime?
Longer answer: It sounds like you have a lot of variables to push into the view. This could be a sign that your code has some room for improvement. If for example you pass the same set of related variables again and again in different controller actions, maybe you can group them into their own model class and build some convenient interface to access all these values. Then all you’d have to do in your controller is to instantiate that model class and push it into the view.
Also keep the "skinny controller - fat model" concept in mind: Put as many data related logic into the model. Controllers should not have to fiddle about your data too much.
Thanks for your response. I’m actually only trying to push one variable into the view so its not an issue of usage, its more personal preference I guess. I’ve used CakePHP, a bit of ZF and also a custom framework using Smarty and in all those cases its possible to push data into the view bit by bit so I guess I’m used to that and find it more convenient.
I guess its not a big issue as I can build up an array, bit by bit, with all the different bits of data that I want to push in and then at the end just push this array into the view. I understand what you are saying and agree with you on skinny controllers.