Yii Framework Forum: Passing data o a view - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Passing data o a view Rate Topic: -----

#1 User is offline   DanielCarrera 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 83
  • Joined: 11-January 09
  • Location:Earth

Posted 24 January 2009 - 08:27 PM

I know of two ways to pass data to a view:

Option 1:

/* Controller */
public function actionIndex()
{
    $this->render('myview', array('message' => 'hello') );
}
/* View */
<? echo $message ?>


Option 2:

/* Controller */
public function actionIndex()
{
    $this->message = 'hello';
    $this->render('myview');
}
public function getMessage()
{
    return $this->getPageState('message');
}
public function setMessage($msg)
{
    return $this->setPageState('message',$msg);
}
/* View */
<? echo $this->message ?>


My question: What is the advantage of the second method?

I thought that the second method would give you some sort of persistence (e.g. cookies) but that doesn't seem to be the case.
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,901
  • Joined: 04-October 08
  • Location:DC, USA

Posted 24 January 2009 - 08:48 PM

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

#3 User is offline   DanielCarrera 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 83
  • Joined: 11-January 09
  • Location:Earth

Posted 25 January 2009 - 05:42 AM

Yes,  got the second method from the hangman demo. Thanks for the explanation. Yes, I see the CHtml::statefulForm(). Now I understand the hangman demo better too.  Thanks.

0

#4 User is offline   Ziad 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 31-October 10
  • Location:London

Posted 31 October 2010 - 05:59 PM

Hi,

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?

Thanks

View Postqiang, on 24 January 2009 - 08:48 PM, said:

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.

0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 01 November 2010 - 05:29 AM

View PostZiad, on 31 October 2010 - 05:59 PM, said:

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?



Short answer: No.

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

#6 User is offline   Ziad 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 31-October 10
  • Location:London

Posted 01 November 2010 - 06:41 AM

Hi Mike,

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.

Thanks

View PostMike, on 01 November 2010 - 05:29 AM, said:

Short answer: No.

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.

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users