CodeIgniter Forums

Full Version: Is it okay to use variables directly in view files ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Grim[/eluser]
Recently I extend the CI_Controller and have added variables which hold user info. But instead of using
Code:
$data['user_info'] = $this->data->user_info;
$this->load->view('profile', $data);
Can I just use it directly in view files like so...
Code:
if($this->data->user_info) echo '<a href='/logout/'>logout</a>';

Thanks

El Forum

[eluser]pickupman[/eluser]
It just looks cleaner to send them to your view. If you would like to pass the data from your construct to your view you can use $this->load->vars( ).

El Forum

[eluser]Grim[/eluser]
[quote author="pickupman" date="1347806207"]It just looks cleaner to send them to your view. If you would like to pass the data from your construct to your view you can use $this->load->vars( ).[/quote]

So there are no drawbacks to it ? I'll look into $this->load->vars( ) Thanks !

El Forum

[eluser]TWP Marketing[/eluser]
[quote author="Grim" date="1347803940"]Recently I extend the CI_Controller and have added variables which hold user info. But instead of using
Code:
$data['user_info'] = $this->data->user_info;
$this->load->view('profile', $data);
Can I just use it directly in view files like so...
Code:
if($this->data->user_info) echo '<a href='/logout/'>logout</a>';

Thanks [/quote]
Two points:
First, your view may not be in the scope of the current instance and $this->xxx may not be available when the view is processed by php. This could happen if you are saving your rendered view as an HTML string for later use, or have cached the rendered view.

Second, In the MVC paradyme, the reason for doing your data retrieval in the controller, via the model, is to avoid a future situation in which you might change the code in the controller/model. You shouldn't need to modify your view code, only change the model code which generated the data item.

For example, in the code sn
ippet above:
Code:
$data['user_info'] = $this->data->user_info;
$this->load->view('profile', $data);
The data item 'user_info' came from someplace (not shown in your example). Probably it was read from a database (in the model) or was entered by the user on an HTML form (processed in the controller). You should pass the finished data set to the view for display.

All that being said, it is only php code and you can get away with using data directly from the current instance of the object ($this->somevar). Most of the time, but it breaks the MVC paradyme used by the CI framework. Some later programmer is going to have to figure out what you did, or even yourself, a year later, trying to understand your old code. Better to stay within the MVC model.

El Forum

[eluser]Grim[/eluser]
[quote author="TWP Marketing" date="1347813509"][quote author="Grim" date="1347803940"]Recently I extend the CI_Controller and have added variables which hold user info. But instead of using
Code:
$data['user_info'] = $this->data->user_info;
$this->load->view('profile', $data);
Can I just use it directly in view files like so...
Code:
if($this->data->user_info) echo '<a href='/logout/'>logout</a>';

Thanks [/quote]
Two points:
First, your view may not be in the scope of the current instance and $this->xxx may not be available when the view is processed by php. This could happen if you are saving your rendered view as an HTML string for later use, or have cached the rendered view.

Second, In the MVC paradyme, the reason for doing your data retrieval in the controller, via the model, is to avoid a future situation in which you might change the code in the controller/model. You shouldn't need to modify your view code, only change the model code which generated the data item.

For example, in the code sn
ippet above:
Code:
$data['user_info'] = $this->data->user_info;
$this->load->view('profile', $data);
The data item 'user_info' came from someplace (not shown in your example). Probably it was read from a database (in the model) or was entered by the user on an HTML form (processed in the controller). You should pass the finished data set to the view for display.

All that being said, it is only php code and you can get away with using data directly from the current instance of the object ($this->somevar). Most of the time, but it breaks the MVC paradyme used by the CI framework. Some later programmer is going to have to figure out what you did, or even yourself, a year later, trying to understand your old code. Better to stay within the MVC model.[/quote]


Thanks I'll take your advice and stick with strict MVC layout. :-)