CodeIgniter doesn't like methods in views? CodeIgniter doesn't like methods in views? codeigniter codeigniter

CodeIgniter doesn't like methods in views?


Define your functions in a helper and load them from the controller. That way you can reuse the functions in other views, as well.


I'm not familiar with CodeIgnitor, but it could be including your templates multiple times. Try wrapping your function in a check:

if (!function_exists('myfunc')){    function myfunc() {}}

CodeIgnitor is probably swallowing errors, so you could also try flushing buffers immediately before your function:

while(ob_end_flush()){}error_reporting(E_ALL);ini_set('display_errors', 1);

In reality though, you should probably make your string formatting code a bit more general. Your template is not really a good place to start adding functions. You'll begin duplicating code, and it defeats the purpose of having templates at all. I'd suggest experimenting with CodeIgnitor's Helpers and Plugins


Views are not meant to call controller actions. Reverse your logic, call that function in the controller and set it to a variable you sent to the view. Then you can have the if statement check that variable in your view template.

If that doesn't work for you, maybe a helper is what you need: http://codeigniter.com/user_guide/general/helpers.html