Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter helper in the view

Is it a good workaround and would it be possible to use helper classes in the view, in CodeIgniter. I have a situation when I have to extract with regulars expression from a text couple of strings and generate outputs on matches. I would not like to do this directly in the view and I would like to use for this purpose a helper.

application
--view
---myview.php

and here I should call the helper and return results

for example I want to extract from the text the type of processor, than I pass the text and get returned the processor type. This one is needed because all the data in the view are generated by an API dynamically.

echo $myhelper->processor($text);
like image 843
fefe Avatar asked May 12 '13 17:05

fefe


People also ask

How do I load helper into view?

php, you would do this: $this->load->helper('url'); A helper can be loaded anywhere within your controller methods (or even within your View files, although that's not a good practice), as long as you load it before you use it.

How do you call helper function in CI controller?

To use helper files, you need to load it. Once loaded it is globally available to your controller and views. They are located at two places in CodeIgniter. CodeIgniter will look first for a helper in application/helpers folder and if not found there then it will go to system/helpers folder.

What is helper function in CodeIgniter?

They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions. CodeIgniter does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.

What is difference between Library and helper in CodeIgniter?

The main difference between Helper and Library in CodeIgniter is that Helper is a file with a set of functions in a particular category and is not written in Object Oriented format, while the Library is a class with a set of functions that allows creating an instance of that class and is written in Object Oriented ...


1 Answers

CodeIgniter's user guide explains that helpers can be loaded and their function used in views.

CodeIgniter does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.

However it is not best pratice to load a helper in a view, so you could either auto-load the relevant helper, or load it in your controller(s).

A helper can be loaded anywhere within your controller functions (or even within your View files, although that's not a good practice), as long as you load it before you use it. You can load your helpers in your controller constructor so that they become available automatically in any function, or you can load a helper in a specific function that needs it.

So, using helper functions in a view is fine, although it is encouraged that the helper is loaded in a controller, or auto-loaded.

like image 193
jleft Avatar answered Sep 22 '22 10:09

jleft