2

I'm not able to pass a simple variable to the view , can someone tell me what I'm doing wrong ?

I have a controller user :

class Controller_User extends Controller_Template_Login {  

     public function action_index()
    {
        $this->template = 'user/info';
        parent::before(); 
        $user = Auth::instance()->get_user();         
        $this->template->content = View::factory('user/info')
            ->bind('user', $user)
            ->bind('message', $message)
            ->bind('errors', $errors);        

        // if a user is not logged in, redirect to login page
        if (!$user)
        {
            Request::current()->redirect('user/login');
        }
    }

}

Inside my view (user/info) i get that user is not defined .

what I'm missing ?

Edit : Note that this can be fixed by adding the :

$user = Auth::instance()->get_user(); 

to the Controller_Template_Login

        $user = Auth::instance()->get_user();
        $this->template->user = $user;

What i want to know is why this doesn't work :

    $user = Auth::instance()->get_user();         

    $this->template->content = View::factory('user/info')
    ->bind('user', $user)
    ->bind('message', $message)
    ->bind('errors', $errors); 
Tarek
  • 3,810
  • 3
  • 36
  • 62

1 Answers1

4

Because in the first case

$this->template->user = $user;

you assign $user variable to a user property of $this->template.

And in the second case:

$this->template->content = View::factory('user/info')
    ->bind('user', $user)
    ->bind('message', $message)
    ->bind('errors', $errors); 

you bind $user variable to a user property of $this->template->content.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • So the following code does nothing : $this->template->content = View::factory('user/info') , it only assign the content of the view to template->content but its not used right ? – Tarek Apr 13 '12 at 11:29
  • 1
    @Tarek: this code assigns a view object to the `content` property. If you use `$content` variable in the template - it would be rendered. If not - it doesn't do anything useful – zerkms Apr 13 '12 at 12:50