Head Library

Difficulty: Easy

Hi I’m Matt, a web developer based in the UK and I’ve been using Kohana for a few months now.

One of the great things about Kohana is it’s employment of clean Object Oriented coding strategies, as such it makes it easy for us to add any functionality we wish.

I had been playing with the concepts of Modularity within Kohana and wanted a way to unify objects access when writing out to the Head of the document, so I came up with the simple library below;

[View the library here]

As you can see there is nothing complex at all, it basically provides a protected method for writing to an internal static variable, it then provides a number of static wrapper classes to add specific items to the internal object. This object is then converted into a string using the _toString method at the end whenever we require it.

Combining this library with the template controller makes it very easy to then provide unified access (through the static methods) to the document head of the outputted page from wherever you are in the system.

First of all simply place the above library in your application/libraries folder, and call it Head.php. Then placing the following line in your site controller (any controller that your pages extend from) instantiates the object;

//Attach the head object
$this->template->head = Head::instance();

This makes the object available to the template view in the variable named ‘head’. It is then simply a case of outputting this variable from within your view:

<!-- Dynamically generated head follows -->

And your done! You can now use any of the functions from throughout your application:

Head::title("Login to the application!");
Head::incCss("public/styles/main.css");
Head::incCss("public/styles/login.css");

There is certainly a lot of scope to extend this class, or provide some stricter internal implementation, such as enforcing uniqueness of stylesheets, js’s etc so that they are not included multiple times, you could even include specific dependency rules which checked when including certain media etc etc.

I hope that it may be of help to someone, or even serve as a small demonstration of how easy it is to implement your own functionality within the Kohana framework. If you have any questions/comments then please leave them below or contact me at matt[at]ninjapenguin[dot]co[dot]uk.

~Matt


5 Responses to “Head Library”

  1. James Says:

    yay! A new article! Thank you Matt!! :P

  2. Xobb Says:

    Nice article and really good lib. The only thing is coding style we use in kohana. You can read about them here: http://trac.kohanaphp.com/wiki/CodingStyle

  3. RastaTech Says:

    Matt, this was such a good idea I couldn’t help but take it and run with it. Perhaps I got a little carried away, but here’s my version, which takes your concept a tad further:
    http://code.google.com/p/various-kohana-goodies/
    Let me know what you think!

  4. dlib Says:

    Nice, I’ve had my own Head library for a couple of months now but do not deem it ready for publication. I was planning on posting on it but Matt beat me to it with his own take. I’m glad you like the idea of it. I’ve found it very useful.

  5. Matt Says:

    Hi RastaTech,

    Great work with the library it looks really good, I’ll have a really close look into it over the weekend, whilst it is a simple idea it definitely demonstrates the power of Kohana OO design.

    Good Stuff!

    /Matt

Leave a Comment