CodeIgniter Forums
Calling a helper method from a view - 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: Calling a helper method from a view (/showthread.php?tid=16148)



Calling a helper method from a view - El Forum - 02-25-2009

[eluser]Alninio[/eluser]
Hi everyone,

I have been using CodeIgniter for a little while now and while I seem to be getting on OK with it I have recently run into something of a stumbling block which I am kinda hoping someone can help me with.

I have a query which returns a large number of results from my model and I am passing the results object to my view. My view then iterates through the results and builds the output display, a complex and interactive grid display.

I am using various values returned from the database to provide unique id's for some of the DOM elements so that I can reference and manipulate them. Unfortunately some of the data contains ' and " characters.

These are causing a problem for me and I want to escape them. I know there is a helper function called quotes_to_entities() that will perform the required string replace but I am having difficulty calling it from within my view.

I understand that this may not be best MVC practice but the complexity of splitting the construction of the grid across multiple controller/view iterations is enough to make me want to do it this way.

Any help greatly appreciated.

Many thanks.

Al.


Calling a helper method from a view - El Forum - 02-25-2009

[eluser]macleodjb[/eluser]
you've got to load the helper into your view, then make the call to the function


Calling a helper method from a view - El Forum - 02-25-2009

[eluser]Alninio[/eluser]
OK,

I have tried adding

$this->load->helper(array('string'));

to the top of my view file and it still doesn't seem to be doing anything.

Sorry if I am being super dense.

Thanks.

Al.


Calling a helper method from a view - El Forum - 02-25-2009

[eluser]macleodjb[/eluser]
how are you calling the helper?


Calling a helper method from a view - El Forum - 02-25-2009

[eluser]Alninio[/eluser]
$display_channel_name = quotes_to_entities($row->channel_name);
$display_show_name = quotes_to_entities($row->name);


Calling a helper method from a view - El Forum - 02-25-2009

[eluser]Alninio[/eluser]
OK, I have fixed it by moving the grid builder code into its own helper file, building the grid and passing it into the view as a string.

Its just as bad MVC wise as before because now I have display logic in a helper file but at least it solved the problems I was having with function scope.

Thanks for the help Smile

Al.


Calling a helper method from a view - El Forum - 02-25-2009

[eluser]Colin Williams[/eluser]
Display logic in a helper isn't bad if the View calls that helper.


Calling a helper method from a view - El Forum - 02-25-2009

[eluser]Alninio[/eluser]
Ah!

The reason I put it in a helper is because I was having difficulty calling the functions from the view. If someone can show me a quick code snippet of how a helper is instantiated and then called from a view I would be very grateful. I have tried:

$this->load->helper(array('url', 'setting', 'string'));

followed by

$display_channel_name = quotes_to_entities($row->channel_name);

but it just didn't seem to do anything.

In the end I just copied the strip_quotes() function from the string helper into my own helper, renamed it as MY_strip_quotes() and had it replace ' with \' and " with \"

Not perfect but it works.

I am now the calling the helper from my controller and building the string which I pass into the view to position correctly within the rest of the page.


Calling a helper method from a view - El Forum - 02-26-2009

[eluser]Colin Williams[/eluser]
Either autoload the helpers, or load them in the controller before the View is called.


Calling a helper method from a view - El Forum - 02-26-2009

[eluser]Alninio[/eluser]
Thanks guys Smile

The best thing about CodeIgniter is the excellent level of support provided both by the dev team and the community.

You guys rock.

Best,

Al.