Creating my own helpers? Is this the good way to clean the view? |
[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;?>
[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.
[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.
[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 ![]()
[eluser]InsiteFX[/eluser]
Add to your_helper ./application/helpers Code: // --------------------------------------------------------------------
[eluser]CroNiX[/eluser]
CI already has helpers to build form elements like form_dropdown(). Have you tried that before making your own?
[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?
[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 ![]()
[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. |
Welcome Guest, Not a member yet? Register Sign In |