views:

330

answers:

3

I am trying to accomplish the following. I need to use Drupal 6 as a project requirement, but I want to use it with my own HTML and CSS stylesheets for each node/view/panel etc.

The problem is, whatever the theme, I always found that Drupal applies to my HTML content both my CSS stylesheets and the CSS related to the theme chosen. I have also tried, without success, using the stylestripper module (installed in sites/all/modules). No matter what I do, additional CSS stylesheets are applied to my pages, completely destroying my layout.

What is the proper way to achieve this? Why stylestripper does not work at all? Is there a completely blank theme available? I have tried basic, mothership, zen etc, but I always see additional CSS stylesheets applied to my pages.

This is driving me crazy, Drupal was chosen by someone else for its flexibility. Thank you in advance.

+9  A: 
<?php
/**
 * Helper function to allow easy CSS excludes + includes
 */
function _phptemplate_get_css($exclude = array(), $include = array()){
$css = drupal_add_css();
foreach ($css['all']['module'] as $k => $path) {
   $file = substr($k, strrpos($k, '/') + 1);
   if (in_array($file, $exclude)){
     unset($css['all']['module'][$k]);
   }
}
foreach ($include as $file){
   $css['all']['theme'][path_to_theme() .'/'. $file] = true;
}
return drupal_get_css($css);
?>

Read more at drupal.org.

Update:
The proper place to put this function, is in the template.php file of your theme. Actually in your case you need to pass an array of css filenames which you want to exclude.
The call to drupal_add_css() with no arguments passed, will provide $css with an array of CSS files which are going to be attached to your theme. So this is the right time to get in!

As you see, In the first foreach loop we simply seek the $css array for filenames which are existed in the passed $exclude array, for style deletion. And we do the same job in the second loop for style insertion. And at the end of this, we return a themed representation of all styles that should be attached to the theme making use of drupal_get_css() function. (maybe nothing in your case)

Well, Where to call this function? You can call this helper function in _phptemplate_variables() for D5 or YOUR_THEME_preprocess() for D6. As we see this in action for D6 (untested):

function YOUR_THEME_preprocess(&$vars, $hook){
    // Stylesheet filenames to be excluded.
    $css_exclude_list = array(
        'lightbox.css',
        'lightbox_lite.css',
    );

    // Making use of previously defined helper function.
    $vars['styles'] = _phptemplate_get_css($css_exclude_list);
}

I'm sure you know how to exclude 'em all ;)

Sepehr Lajevardi
Thank you, this appears to be perfect. Can you please add the missing details needed to actually use the function? In which file should I put the function? How to call it to exclude all of the CSS stylesheets?
unforgiven
You're welcome, I'm going' to update the answer ;)
Sepehr Lajevardi
Excellent! This was exactly what I was searching for! Thank you for sharing this!
unforgiven
A: 
Nitz
A: 

I try to use this code but result no-compatible with css compression setting.

If you compress the css in "performance" Drupal page the file result blank!

Luig

related questions