CodeIgniter Forums
When to use a library within the view (maybe?) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: When to use a library within the view (maybe?) (/thread-40849.html)



When to use a library within the view (maybe?) - El Forum - 04-20-2011

[eluser]c77m[/eluser]
I know in an ideal world libraries would not be used inside a view. However, it is also ideal not to have redundant code all over the place, too. So what is the best answer to this conundrum?

I have database-driven data that needs to be loaded on <b>EVERY</b> page in the site, regardless of controller and method. It is in a portion of a sidebar view, which is loaded on each page.

Instead of loading the data in every single controller and passing it to the view, would this not be a good example of when you <i>should</i> access a library inside the view? I'm looking for pros and cons with a focus on code efficiency.


When to use a library within the view (maybe?) - El Forum - 04-20-2011

[eluser]Eric Barnes[/eluser]
From the way you describe it I would be using a helper instead of a library.


When to use a library within the view (maybe?) - El Forum - 04-20-2011

[eluser]toopay[/eluser]
I'll probably configure some hook for that!


When to use a library within the view (maybe?) - El Forum - 04-20-2011

[eluser]John_Betong_002[/eluser]
I sort-of solved a similar problem by calling a model function from the common MY_Controller-&gt;constructor(). The array loaded from the model function is passed to the sidebar view like this:


Code:
// MY_Controller
function __construct()
{
  parent::__construct();

  // update top-10 today
  $this->today = $this->m_lib->today_hits_get_links();    
  ...
  ...
  ...




// sidebar view
  if(isset($this->today) && count($this->today) >= 2)
  {
    echo heading('Most popular hits last 24 hours',5);
    echo ol($this->today, $attributes);

}else{
  // the "best laid plans of mice and men often go awry
  ...
  ...
  ...
}
&nbsp;
edit:
Unfortunately; introducing database caching has opened up a whole can of worms and the bl**dy list does not get updated Sad
&nbsp;
&nbsp;