7

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);
tereško
  • 58,060
  • 25
  • 98
  • 150
fefe
  • 8,755
  • 27
  • 104
  • 180

4 Answers4

12

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.

jleft
  • 3,457
  • 1
  • 23
  • 35
  • thanks for the feedback! If I initialize the helper in my controller than would available as well in my view? – fefe May 12 '13 at 18:14
  • 1
    Yes! - _Once loaded, it becomes globally available in your controller and views._ – jleft May 12 '13 at 18:18
  • 1
    To me, its always better to load resources only when you need it, so load helper in view makes sense. – Thomas Decaux Mar 14 '14 at 14:29
  • 1
    I agree with that principle. However, you can still achieve that by loading the helper in the controller function that loads the view. – jleft Mar 15 '14 at 01:01
6
get_instance()->load->helper('HELPER_NAME');
SachinGutte
  • 6,947
  • 5
  • 35
  • 58
Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
  • 3
    Code-only answers really take away from the post, if someone were to look over this post, they would have no clue what it did – Jojodmo Mar 14 '14 at 14:49
  • 1
    I could explain but there are already lot of comments about "helpers", I am sure this small answer is clear enough. – Thomas Decaux Mar 14 '14 at 14:58
  • That other people have said things (which might be deleted one day) is irrelevant. Every answer should be explained with the intention to empower/educate. Explaining every answer also role models good posting behaviors to newer users. Notice how jleft's answer is explained, has helped more researchers, and has more upvotes. – mickmackusa Oct 10 '20 at 08:33
  • @mickmackusa sorry, I hate copy/paste info from doc, in order to educate, in my mind, this is better to read the full user guide first, then ask precise question about point not covered by documentation. – Thomas Decaux Oct 12 '20 at 08:41
  • If you don't want to explain _how_ it works, then you could also offer compelling arguments why researchers should use this technique over another. You can mention caveats, gotchas, alternative techniques that you wouldn't use and why. Be creative if you don't want to copy-paste. – mickmackusa Oct 12 '20 at 08:56
4

Just load the helper in your controller, then

$this->load->helper('MY_common_functions');
$template['content'] = $this->load->view('your_view');

In the view call your function name directly. In this case I called my convertor function

echo convertor($params);
ganji
  • 752
  • 7
  • 17
0

It is standard in Codeigniter 4, always load Helper function before to use it, Either in Controller or Views. In Codeigniter 4 If we declare Helper function in Controller's __construct method like:

<?php
  namespace App\Controllers;
  class NewsEventController extends BaseController{
  public function __construct(){
        helper('form');
  }

Than this Helper function will available in all controller functions & views of that controller. Example a view file with 'form' helper function 'set_value()' in an input field like:

<input type="text" class="form-control" name="title" value="<?= set_value('title') ?>" >
Zeeshan DaDa
  • 197
  • 1
  • 10