![]() |
Creating my own helpers? Is this the good way to clean the view? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Creating my own helpers? Is this the good way to clean the view? (/showthread.php?tid=56884) Pages:
1
2
|
Creating my own helpers? Is this the good way to clean the view? - El Forum - 01-27-2013 [eluser]behnampmdg3[/eluser] Hello friends; Lates say I want to write some code that creates a drop down that shows months of the year. I can easily write loose php in view which sucks: Code: <select name="post_month"> Controller: Code: $this->load->library('months_drop_down','','drop_down'); Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Code: <?php echo $months;?> Creating my own helpers? Is this the good way to clean the view? - El Forum - 01-27-2013 [eluser]Harold Villacorte[/eluser] You only need to reference the super object to access the native classes. Other than that write like you normally write a php class. Set the the property then access it within the method using $this->property or self::$property. Creating my own helpers? Is this the good way to clean the view? - El Forum - 01-27-2013 [eluser]behnampmdg3[/eluser] Harold, please explain more. Thanks Creating my own helpers? Is this the good way to clean the view? - El Forum - 01-27-2013 [eluser]Harold Villacorte[/eluser] This is a Codeigniter specific forum so I cannot post tutorials on PHP fundamentals. But I can tell you this much, you do not need to devote an entire library to one function. Just put it in a model. CI allows you to load any model from any controller. Creating my own helpers? Is this the good way to clean the view? - El Forum - 01-28-2013 [eluser]PhilTem[/eluser] There's a helper called "form_helper" that creates dropdowns from an array passed as an argument. Look at the user's guide to see, how it works. The only thing you would need to do: Create the array of months in another helper (or extend the form_helper with a MY_form_helper and add a month_dropdown() function that takes care of all this ![]() Creating my own helpers? Is this the good way to clean the view? - El Forum - 01-28-2013 [eluser]InsiteFX[/eluser] Add to your_helper ./application/helpers Code: // -------------------------------------------------------------------- Creating my own helpers? Is this the good way to clean the view? - El Forum - 01-28-2013 [eluser]CroNiX[/eluser] CI already has helpers to build form elements like form_dropdown(). Have you tried that before making your own? Creating my own helpers? Is this the good way to clean the view? - El Forum - 01-31-2013 [eluser]behnampmdg3[/eluser] [quote author="Harold Villacorte" date="1359356266"]But I can tell you this much, you do not need to devote an entire library to one function. Just put it in a model. CI allows you to load any model from any controller.[/quote]But isn't model suppose to look after database actions? Creating my own helpers? Is this the good way to clean the view? - El Forum - 01-31-2013 [eluser]behnampmdg3[/eluser] Thanks for all the replies above. Form helper is a great solution BUT, I have become lazy using all these helpers lol I feel tough to handcode a few things ![]() Creating my own helpers? Is this the good way to clean the view? - El Forum - 01-31-2013 [eluser]Harold Villacorte[/eluser] Well yeah. I think we already both got our answer that stuff like that in basic Codeigniter belongs in a helper. I tend to follow a slightly different pattern because I write everything as an HMVC module with no libraries and no helpers, just modules. And modular CI is not addressed in the documentation. The point I was making was that you were writing a class called Months_drop_down as a library and method called index() to generate a select form. So to call this function you would have to something like: $this->load->library('months_drop_down'); $this->months_drip_down->index(); Wouldn't it be much cooler to call it like this: $this->load->library('my_awesome_library'); $this->my_awesome_library->months_dropdown(); But we already got an answer on where to put helper functions. |