Submitted by Jeff Geerling on December 28, 2010 - 2:21pm
For some time, I've been using the most hackish ways of including custom CSS and Javascript in my site via themes and custom modules. The problem has been (at least, in Drupal 6) that the hook_preprocess_page() function, inside which I'd really like to include my drupal_add_css() and drupal_add_js() functions, due to the fact that it's easy to see what page I'm on, or what kind of page I'm on, is not able to add CSS or JS to the page, due to the order in which the hooks are fired.
I've often just put my JS and CSS includes (via the drupal hooks) into the end of my custom module file, instead of inside a function at all.
However, a much cleaner way to include your CSS and JS is inside your own implementation of hook_init(). For example:
<?php
/**
* Implementation of hook_init().
*/
function custom_init() {
// Add custom CSS & JS to front page.
if (drupal_is_front_page()) {
drupal_add_css(drupal_get_path('theme', 'custom') .'/css/sample.css', 'theme', 'screen,projection', FALSE);
drupal_add_js(drupal_get_path('theme', 'custom') .'/js/sample.js', 'theme', 'header', FALSE, TRUE, FALSE);
}
// Add Javascript to a specific Context
if (context_isset('context', 'blog')) {
drupal_add_js(drupal_get_path('theme', 'custom') .'/js/blog-sample.js', 'theme', 'header', FALSE, TRUE, FALSE);
}
}
?>There! Much nicer. (This code is adapted from a sample sent to me by Joel Stein).
Comments
Note that in Drupal 7 you can
Permalink Submitted by tstoeckler (not verified) on December 28, 2010 - 4:28pm.
Note that in Drupal 7 you can specify CSS and JavaScript files via the module's or theme's info files. If you want to conditionally load certain files (like in the example above) you still have to implement hook_init().
You can add CSS/JS via .info
Permalink Submitted by Jeff Geerling on January 4, 2011 - 2:37pm.
You can add CSS/JS via .info in Drupal 6 as well, but it's often best to only load the files conditionally, for performance reasons.
Check out my personal website: www.lifeisaprayer.com.
I would wrap the call to
Permalink Submitted by mikeytown2 (not verified) on December 28, 2010 - 5:29pm.
I would wrap the call to context_isset in a module_exists(); that's just me though, I try to make code more robust OR set a dependency for context in the info file.
Using hook_init() is
Permalink Submitted by bleen (not verified) on December 28, 2010 - 6:26pm.
Using hook_init() is generally preferred, but for what its worth, if you do need to add a css file in mytheme_preprocess_page(&$vars) ... you can do this:
<?phpdrupal_add_css('path/to/file.css');
$vars['styles'] = drupal_get_css();
?>
The preproces function also
Permalink Submitted by Geoff (not verified) on December 29, 2010 - 9:25am.
The preproces function also works ie... Themename_preprocess if you want to do this in your theme template.Php file
It never seemed to work for
Permalink Submitted by Jeff Geerling on January 4, 2011 - 2:37pm.
It never seemed to work for me, thus the hook_init().
Check out my personal website: www.lifeisaprayer.com.
Nice use of includes, should
Permalink Submitted by Seagrass Headboard (not verified) on March 10, 2011 - 7:05pm.
Nice use of includes, should save a few KB and speed up the load time.
I found the perfect place for
Permalink Submitted by buddies (not verified) on July 14, 2011 - 12:47pm.
I found the perfect place for my needs. Contains wonderful and useful messages. I have read most of them and has a lot of them. To me, he's doing the great work.
Foil Insulation
Add new comment