![]() |
Calling library methods 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 library methods from a view (/showthread.php?tid=14106) |
Calling library methods from a view - El Forum - 12-18-2008 [eluser]thody[/eluser] Is it philosophically legit to call a library method directly from a view, or should it be done via a helper method? What are the advantages/disadvantages of each approach? Calling library methods from a view - El Forum - 12-18-2008 [eluser]nhm tanveer hossain khan (hasan)[/eluser] view should be as loosely coupled as possible. if you put your library loading code on view, which means you are creating dependency on the specific library from the view. view is suppose to be as simple and neat as possible to render the output you expect. i would rather prefer using helper method to do this job. because later it would give me more flexibility to switch any library i want. best wishes, ![]() Calling library methods from a view - El Forum - 12-18-2008 [eluser]thody[/eluser] Actually, to be more specific, the situation I have in mind is for a global UI element, so the library would be loaded in autoload.php, and the only code in the view would be the fucntion call. For example: Code: <ul> Calling library methods from a view - El Forum - 12-18-2008 [eluser]nhm tanveer hossain khan (hasan)[/eluser] gotcha ![]() i don't think that is harming core philosophy so much since you are not writing the whole logic which was written under "get_categories" method. though you could load them under controller and send them over a variable. (which MVC suggests) i am pretending this bunch of view code is used for creating modular component. which is not actually controller action dependent. i have followed the following method in my code - Code: <?= render_categories() ?> on helper you should have the following code - Code: function render_categories() { On common/categories.php Code: <ul> it gives me chance to separate my logic and view with in the component scope. so later if i decide to change the logic in the category retrieval process or use separate library i don't need to touch the view. best wishes ![]() |