Skip to content

Persistent Content Across Views

Derek Jones edited this page Jul 5, 2012 · 5 revisions

Category:Approaches::Header and Footer and Menu on every page

Overview A very simple but neat way to add some "constant" content to every page by overriding the "view" function in CI's "Loader" class. This enables content such as headers / footers to be added to some or all pages without hacking any of CI's system files or changing all your existing controllers.

Approach (brief) The ubiquitous $this->load->view() function is overridden using CI's inbuilt mechanism for dynamically replacing it's code (i.e. http://codeigniter.com/user_guide/general/core_classes.html extending core classes[/url]).

The basic method is very easy (just a few lines of code) and is ripe for customising further. For example, code could be added to alter the static pages that are provided (instead of turning the function on or off as is presently the case).

Approach (in detail) The code below is valid when the following assumptions are made:

  • A complete page can be constructed from three views, "header", "content" and "footer"
  • You haven't changed the config variable 'subclass_prefix' from 'MY_'

Simply create a new file in 'application/core' named 'MY_Loader.php' and add the following content:

You can embellish with more comprehensive comments (you might want to copy those from the original function as a starting point), but I've kept it brief here on purpose.

Then simply call "view()" in your controller as you normally would for your content:

$this->load->view('content', $data);

What's Going On As seasoned coders will easily see, we've derived a new class from "CI_Loader" in which we've overridden the existing function, "view()". A feature of CI is that you can replace any of the existing core classes by creating a class called "MY_xxx" in the 'application/core' folder (where "xxx" is the name of an existing CI class, but without the "CI_" prefix). Dropping this derived and appropriately named class in this folder is therefore all we need to do for it to be used every time CodeIgniter is started instead of the original. The only function that has been redefined is "view()", so all others work as before.

When it executes, the original function in the parent class is called three times (in this case) with the header, whatever main content you want and then the footer. Concatenation of the results is done because view can be requested to return the page as a string instead of being sent to the user's browser. The added fourth parameter ($include_static) is used as an indicator for situations where static content is not desired. You will see that the additional calls are conditional upon this being TRUE.

I developed this against the 2.1.0 version of CodeIgniter. It may need changing (or may not work at all) for versions prior to 2.0.0 and, of course, future versions are also subject to change (although I'd expect the approach to be reasonably robust).

Downsides The main problem with this technique is that things become more complicated if you need to construct pages from more than one view: every call to view() will add all the static content or none of it to the output page (depending on the value of $include_static). It would be relatively easy to swap $include_static for a variable used to identify which of several additional code snippets should be introduced (so the first call could include the header and the last the footer), but as it becomes more and more complicated, the advantages of using a technique such as this over and above simply calling view() in your code separately for each snippet become less and less clear.

From a stylistic point of view, some people may have an opinion on creating a very specific version of one of CI's core classes (i.e. one with hardwired views). A more generic approach, using a config file to wire up the static content and a more sophisticated selection process might be an interesting project, but it would be easy to get away from the fact that this is a simple solution for a not very onerous problem.

Clone this wiki locally