Welcome Guest, Not a member yet? Register   Sign In
loading helpers inside views
#1

[eluser]Roopam[/eluser]
I've read this in the manual which says that although one can load a helper in a view, its not a good idea. Can someone please tell me why?
#2

[eluser]benton.snyder[/eluser]
One of the main goals of the MVC design pattern is to separate the front-end display code from the back-end business logic code. At the very least, using a helper inside of a view would violate this tenant.
#3

[eluser]PhilTem[/eluser]
The title is a little misleading, but to answer your question: Helpers are actually wrappers for views to call functions from libraries, models, or the controller. Since you don't want to have something like

Code:
echo $this->input->post('field_name');

in a view but something like

Code:
echo post_value('field_name');

is okay to have. This is also important since you can't ensure that the scope of $this in the view is the same as in the controller (i.e. the CI super object).

Using helpers in your view code is good and suggested, using them in libraries or the controller is okay, as long as these helpers are generic helpers, i.e. don't manipulate or use the CI super-object. (That's all imho)
#4

[eluser]Roopam[/eluser]
@benton.snyder Umm, I think its not about separating the backend from the front end. The "back end" I think refers to database related operations. I am referring probably to something like formatting strings (converting to camel case etc), date time calculations and the like

@PhilTem "Helpers are actually wrappers for views to call functions from libraries, models, or the controller" Well, this calls for extra code : writing helpers to get me the post values since we are not sure $this object is available in the view. Frankly this is heartbreaking, since I have been doing stuff like $this->config->item('fb_app_key'); for creating fb login javascript codes. But yes I do get the vibe here.

My question was in terms of loading the helper inside a view. Let me elaborate:
I have two options of loading the helper
1. From the controller which loads the view. And therefore the helper functions will be available to the view and the rest of controller code.
2. From the view, which might just load the helper only for the view and discard it for the rest of the controller.

My question is in the second point. Is my assumption right? As in, will it be more memory optimized to load the helper inside the view rather than the controller.
And sorry for my misleading title. Sad I'll mind this for future posts. Thanks
#5

[eluser]PhilTem[/eluser]
To answer the second question first: If you load it in the view there is no real memory optimization as the helper will be available to the controller afterwards, too. It's just at a later point in time that you load the helper.

I personally load all my helpers in the controller (or in autoload.php) which is imho a good way to do it, since the view only needs the functions inside the helper and does not need to know how to load helpers (that implies separating the business logic from the presentation logic). I may, though, be wrong with my approach, but I can't remember having seen any $this code in e.g. PyroCMS' or Bonfire's views.
#6

[eluser]InsiteFX[/eluser]
As PhilTem stated I load also load my helpers through the autoload.php file.

My assets_helper
Code:
<link href="<?php echo css_url('prettify.css'); ?>" rel="stylesheet">

As you can see there is no $this on the method name.
#7

[eluser]Aken[/eluser]
[quote author="Roopam" date="1356370780"]@benton.snyder Umm, I think its not about separating the backend from the front end. The "back end" I think refers to database related operations. I am referring probably to something like formatting strings (converting to camel case etc), date time calculations and the like[/quote]
Back end refers to server-side code, PHP in our case. Front end refers to HTML, CSS, and JS - client-side code. The goal is to mix as little PHP with your HTML and such as possible.

[quote author="Roopam" date="1356370780"]@PhilTem "Helpers are actually wrappers for views to call functions from libraries, models, or the controller" Well, this calls for extra code : writing helpers to get me the post values since we are not sure $this object is available in the view. Frankly this is heartbreaking, since I have been doing stuff like $this->config->item('fb_app_key'); for creating fb login javascript codes. But yes I do get the vibe here.[/quote]
$this has a much different scope inside the view than it does in your controllers and models. Another good reason to separate your logic.

For helper functions you use in your view(s), load them globally (autoload) or in your controller, depending on how often you need them.




Theme © iAndrew 2016 - Forum software by © MyBB