CodeIgniter Forums
Is it okay to use variables directly in view files ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Is it okay to use variables directly in view files ? (/thread-54598.html)



Is it okay to use variables directly in view files ? - El Forum - 09-16-2012

[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


Is it okay to use variables directly in view files ? - El Forum - 09-16-2012

[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( ).


Is it okay to use variables directly in view files ? - El Forum - 09-16-2012

[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 !


Is it okay to use variables directly in view files ? - El Forum - 09-16-2012

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


Is it okay to use variables directly in view files ? - El Forum - 09-17-2012

[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. :-)