Welcome Guest, Not a member yet? Register   Sign In
When to use a library within the view (maybe?)

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.

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

I'll probably configure some hook for that!

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:

// MY_Controller
function __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);

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

Theme © iAndrew 2016 - Forum software by © MyBB