Welcome Guest, Not a member yet? Register   Sign In
document_head class
#1

[eluser]TheFuzzy0ne[/eluser]
Hi everyone. I'm thinking about creating a document head class, which will manage everything between the <head></head> tags. When you want to render the page, it will return a string containing everything you've added contained between <head></head> tags.

It will support functions such as addMeta, addScript, AddCss etc...

Some functions will have an alternative which works in a different way. For example, addScript will have a similar function named addScriptFile.

addScript will simply take a heap of JavaScript code (which can be dynamically generated, or read from a file), and include it inline, between tags.

addScriptFile will essentially be the same, only it will take a URL to the JavaScript file, which will be inserted again in script tags, but it will add the "src" attribute to it.

Would anyone like to use anything like this, or am I not thinking along the right lines here? Personally, I think it allows for much more dynamic content, and a convenient place to keep everything stored until rendering time.

I'd be interested to hear your thoughts.
#2

[eluser]xwero[/eluser]
There are a few templating libraries that already possess that feature. I wonder how you use it outside the context of a template? I think creating a head helper with the functions you suggest would be more flexible.
#3

[eluser]TheFuzzy0ne[/eluser]
You would add CSS, meta data and JavaScript etc... passed to it from your controller. The logic in the controller is entirely up to you. The CSS can be added depending on the user's theme, you can even generate dynamic JavaScript, and stick that into it. An example I can think of regarding JavaScript would be if you wanted slightly different JavaScript functions available to administrators, than you would for a normal user. You could also generate a locale array for a script to use if you wanted. The only other alternative would be to hardcode and maintain several versions of the same script. It would also allow you to generate meta data specific to the page.

Yes, you can do this through your template using variables and hardcoded stuff, but I like the idea of putting everything <head>-related into one place, and not having to pass variables around from function to function. IMHO, it results in cleaner, more understandable code, which is easier to maintain, and gives a lot of control over the document, without having to learn complex libraries.

When the template is called upon and parsed, a simple $this->doc_head->getHead() will return the entire head string.

This isn't intended to be a big library. Just a single-class simple library that's easy to use and convenient.




Theme © iAndrew 2016 - Forum software by © MyBB