tags:

views:

330

answers:

3

Hi there,

I'm trying to add stylesheets conditionally to my drupal 6 site. I've added the following code to my template.php file to test it, and it's not working. The css file does not get added to my site.

function ben_preprocess_page(&$vars) {
drupal_add_css('/sites/all/themes/ben/advice.css','theme','all',FALSE);}

This code works fine when I put it in a custom module I made using hook_init() but I think I need to put it in my template.php file as I want to check the page I'm on.

Any help would be much appreciated.

Ben

+2  A: 

IIRC, the stylesheet links have already been rendered when the *_preprocess_page() functions get called, and the generated markup got placed within $variables['styles']. So it is to late to use drupal_add_css() by then.

You could either assemble the <link ...> markup for your additions yourself and append it to $variables['styles'], or you need to find a better place for your call to drupal_add_css() earlier in the processing chain (probably from within a module).

What place that would be is hard to tell without knowing what you mean exactly by 'check the page I'm on', but if we are talking node pages, hook_nodeapi() would be a candidate.


EDIT after clarification in comments: If the decision on what stylesheets to add is based on the path alone, hook_init (in a custom module) would be a proper place to do it, as the path will not change after that. The only 'tricky' bit in this case would be to get at the clean URLs. If (as I assume) you use clean URLs, you can not use arg(0) to get the first element of the path, as it would return the first element of the Drupal internal path (e.g. 'node' for node pages). So you have to get the clean URL version first:

// Get current path alias, if any (will return original path, if no alias set)
$path = drupal_get_path_alias($_GET['q']);
// Extract first element
$path_elements = explode('/', $path);
// Do we have at least one element?
if (0 < count($path_elements) {
  // Yes, add stylesheet based on that
  switch ($path_elements[0]) {
    case 'advice':
      drupal_add_css('path/to/advice.css');
      break;
    case 'services':
      drupal_add_css('path/to/advice.css');
      break;
    // TODO: Add other variations ...
    default:
      // Might add a default alternative here
      break;
  }
}

(NOTE: untested code, beware of typos)

Henrik Opel
Sorry for not being more specific about the 'page i'm on'. I want to apply different styles (background images and some other colouring) to different sections of my site. The different sections are accessed via the primary menu, so the different sections will be decided by the url path. Eg /advice, /services, /events etc. Does that help decide what place would be best to put the call in?
Ben
Thanks very much for your answer. I managed to create a solution last night. I created my own variable using a pre-process function using the menu path and then adding that variable to my page.tpl.php. I use this variable as a class for css. I'll post the answer below.
Ben
+2  A: 

I put the following code in a custom module and it is now appending the css file:

function defprofile_preprocess_page(&$vars) {
drupal_add_css('sites/all/themes/ben/advice.css','theme','all',FALSE);
$vars['styles'] = drupal_get_css(); }
Ben
Exactly, that's the solution. However, the solution does not lie in the fact that you put this in a custom module; this code would work in template.php as well. The key to solving this problem is using drupal_get_css to update $vars['styles'] after you added the css.
marcvangend
I did try putting the exact same code (changing the function name though to fit my theme) into my template.php and it didn't work...
Ben
+2  A: 

I used this code to solve my problem:

function ben_preprocess_page(&$vars) {
        $alias=drupal_get_path_alias($_GET['q']);
        $alias=explode('/',$alias);
        $vars['ben'] = $alias[0]; } 

I can then add $ben to my page.tpl.php inside the body class and make up css class rules based on the different values. e.g. If the path is 'advice' I create a css rule called '.advice'.

Thanks for everyones help, it helped me solve the problem.

Ben

Ben
+1 for posting follow up with used solution
Henrik Opel