Templating App Forms - 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: Templating App Forms (/showthread.php?tid=59922) |
Templating App Forms - El Forum - 12-10-2013 [eluser]xtremer360[/eluser] I'm trying to develop a series of forms with my template that I am using for my Codeigniter application. I am using the form helper to put together my forms. I would like to cut down on a lot of code that is similar on all forms by making some sort of function that will run when a form is to be created. For example: Code: <?php echo form_open('register', array('id' => 'register_form')); ?> What I want to figure out is how I can use a helper function to help create my forms. All forms include the divs that has classes and and forms with form inputs and labels that share the same classes. Can someone show me the right direction as to how this could be accomplished? Templating App Forms - El Forum - 12-10-2013 [eluser]CroNiX[/eluser] I don't see how you could simplify it much. Yes you could make a helper that outputs the divs with the label and field, but you have other variables. Like you'd have to tell it what kind of field since you are using regular "input" and "password" fields, and probably want others that you aren't showing like textarea, dropdowns, checkboxes, etc. So by the time you create a helper you'd probably have just as much code in order to pass the data to the helper, so I'm not sure it would be worth it. And you'd still also have to pass the fields attributes as they change for each input. One thing I would generally advise is to not create a variable unless it is going to be used more than once. It can be argued that an exception would be for readability, like a very long array or something. When a variable is created, it takes memory to store that variable and value. For example, Code: $attributes = array('class' => 'strong'); You aren't using $attributes anywhere else. You are just creating it to pass it, which takes more memory than: Code: echo form_label('Password', 'password', array('class' => 'strong')); Templating App Forms - El Forum - 12-10-2013 [eluser]xtremer360[/eluser] This is true and I will fix this. The reason I bring this question up is in the event in the future if I have themes that have different class names attached to elements or what not so that I can have a form that has the same code base that can be used across different themes. Templating App Forms - El Forum - 12-10-2013 [eluser]CroNiX[/eluser] Well, you can probably narrow it down a little by doing something like Helper Code: //$labelText = Label to display Use like: Code: //create the form element which would replace this: Code: <div class="form-group"> |