Function to generate this code (with variables) - 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: Function to generate this code (with variables) (/showthread.php?tid=11693) Pages:
1
2
|
Function to generate this code (with variables) - El Forum - 09-19-2008 [eluser]bapobap[/eluser] Hi there, I'm making lots of form items and they are pretty much standardised, so I thought why not make a nice helper function to make it simpler but I don't know how to have the variable I need to use in the actual code. Example: Code: <?php if ($this->validation->VAR_error): echo '<div class="error">'; else: echo '<div class="item">'; endif; ?> Somehow generate that, replacing the variables, using a function call like this: Code: <?=nice_form_item('email', 'text', 'Email address')?> Is there any way to do this, or maybe even a better way? Thanks! Function to generate this code (with variables) - El Forum - 09-19-2008 [eluser]Michael Wales[/eluser] Code: function nice_form_item($name, $type = 'text', $label = 'Input') { Function to generate this code (with variables) - El Forum - 09-19-2008 [eluser]bapobap[/eluser] Thanks Michael, where would this go, as I think if it's a helper, it can't access the CI object and this the validation stuff? Function to generate this code (with variables) - El Forum - 09-19-2008 [eluser]Pascal Kriete[/eluser] You can get access to the CI object from the outside by using get_instance . Assuming of course, that a controller has been instantiated at this point (so don't try it in the error_404 template). Code: $CI =& get_instance(); Function to generate this code (with variables) - El Forum - 09-19-2008 [eluser]bapobap[/eluser] Hi inparo, How do I pass that to the helper? If I have the function call in my view, that function in the helper, how would I pass that? Function to generate this code (with variables) - El Forum - 09-19-2008 [eluser]Pascal Kriete[/eluser] You don't pass it, you just call it in the helper. It's a function that is 'include'-ed along with the CI_Base object. It's in the global namespace so you can use it anywhere. Check out system/helpers/date_helper.php for an example. Function to generate this code (with variables) - El Forum - 09-19-2008 [eluser]bapobap[/eluser] I must be doing something wrong, I'm getting Message: Undefined property: CI_Input::$validation, I have validation on autoload but tried loading it after the get_instance, neither worked. Function to generate this code (with variables) - El Forum - 09-19-2008 [eluser]Pascal Kriete[/eluser] Can you reduce the error to a small(ish) snippet of code and post that? Function to generate this code (with variables) - El Forum - 09-19-2008 [eluser]bapobap[/eluser] I shall try! Controller Code: function test() Helper (autoloaded, along with validation) Code: function nice_form_item($name, $type = 'text', $label = 'Input') { If I have cut this down to make it stupid sorry, I basically had a big registration method in the controller, the view, then this in the helper. Function to generate this code (with variables) - El Forum - 09-19-2008 [eluser]Pascal Kriete[/eluser] Code: $CI->input->validation->$field_error [Edit: You can blame it on Ssgt Wales ] |