« Oi, Mind your Messages Sync Web Dev with MAMP and Dropbox »
Tags: CodeIgniter, head, html, libraries, meta, PHP, webdev, wrapup
Posted in Code, CodeIgniter, PHP
I’ve been using this library, or iteration of, for about a year now and have finally had time to document it properly. WrapUp is a CodeIgniter Library to manage your sites <head> element.
Defining the doctype
Setting the page title
Adding meta data
Adding CSS
Addming Javascript files
This saves alot of time getting a site up and running. Its also extremly handy for large sites as you can define your default meta data, css & javascript in a __constructor so its is inhereted by all pages on the site.
A few examples of WrapUp can be found after the jump.
Load the library, you’ll probably want to autoload this if you are using it on the whole site.
$this−>load−>library('wrapup');
The WrapUp library enables you to quickly get an HTML page up and running.
$this->wrapup->head();
The head() function returns the basic html head structure as a string.
There’s not a lot to it but from here you can easily customise your head page, set up defaults and overide/add elements for specific pages.
I normally create a MY_Controller which all my classes inherit from. Then all my default head elements, css links, js files are added to the wrapup library in the MY_Controller constructor.
// set doctype to transistional
$this->wrapup->set_doctype('t');
// add charset meta tag
$meta = array(
'http-equiv' =>'Content-type',
'content' => 'text/html;charset=UTF-8'
);
$this->wrapup->add_meta(meta);
// add default css stylesheet
$this->wrapup->add_css('style.css');
// add google jquery script
$this->wrapup->add_js('jquery.min.js');
// add a default page titles
$this->wrapup->set_title('My Page Title');
This sets up our default page head element. I find it saves me a lot of time if I ever need to change the default style sheet for instance or the doctype. I only have to change it in one place!
Another feature of WrapUp is managing inline javascript and CSS. I try not to include any inline CSS due to the dreaded FOUC but its useful for debugging. Using WrapUp to manage you javascript enables you to generate javascript programatically (include server side variables) and group all off your javascript and send it to the browser at the bottom of the page right before closing your body tags.
You can add inline javascript by calling the add_js_inline() WrapUp method.
$js = 'function say_hello(){alert("hello");}';
$this->wrapup->add_js_inline($js);
Inline code can then be returned in your view by calling inline() method
$this->wrapup->inline();
So that’s about it for an introduction to WrapUp, no doubt i’ll add/tweak it as I discover news needs. Checkout the full documentation in the code vault to see other methods like add_rss()
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Stuck Together With Tape is proudly powered by WordPress 3.0.1 Entries (RSS) Comments (RSS).
Perhaps I am missing something, but a library seems overkill for this. You can get the same functionality of having one place to change your header sitewide simply by having a base controller class (i.e. using MY_controller) and have a method on there to ahndel the basic heder settings, then simply include the header in its own view or use a master template.
I agree, this is all possible by including a common head view in your main views.
However I find it helpful, especially in the development stages, to be able to customise your page head from within controllers independently. But for a live site it’s probably worth using static head code to speed up performance.