Passing vars to view

For a long time I used this method of passing variable to view:


public function index(){
    $this->set('items', $this->paginate());
    $this->set('promos', $this->Promo->getRandomPromos(5));
}

OR


public function index(){
    $items = $this->paginate());
    $promos = $this->Promo->getRandomPromos(5);
    $this->set(compact('items', 'promos'));
}

recently I found that $this->data array of controller is passed to view, so basicly I achive similar effect with this


public function index(){
    $this->data['Items'] = $this->paginate();
    $this->data['Promos'] = $this->Promo->getRandomPromos(5);
}

In view though, you can not access it as local var, so you have to use $this->data to access value.

Basicly my questions is how you do that, and is there is any catches of using $this->data as container for passing vars?

Asked by DmitriySpb, on 15/12/09

1 Answer

Hi,

$this->data primary usage is forms : it permit to automagically set inputs values, from values stored in $this->data ( $form->input('User.name') => $this->data['User']['name'] )

So, I only set $this->data with values I have to edit in my forms. For others usages (navigation data, for example), I always use $this->set(), wich is the good method I think :)

Answered by scharrieron 15/12/09

<< previous next >>

Tagged with

Rating

0

Viewed

453 times

Last Activity

on 15/12/09