Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a custom module which contains a template directory and a .tpl.php. I would like to add a specific stylesheet for this template. Right now, my tpl-file looks like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>My Title</title>
 <link rel="stylesheet" type="text/css" href="sites/all/themes/mytheme/css/styles.css" />
</head>
  <body>
    <div class="content-container">
        <p>My content</p>
    </div>
  </body>
</html>

And it works, but only partially... How can I include the css stylesheet in a more proper (read: more Drupal) way? I've tried the drupal_add_css(), but with no results... But maybe I did it the wrong way. Any suggestions?

share|improve this question
1  
Consider putting <html> in new line, now it looks like you forgot it on first glance. –  Mołot Aug 20 at 10:45

1 Answer

In mymodule.info file, add following line:

stylesheets[all][] = css/styles.css

Then, in html.tpl.php be sure to print scripts and styles, like that:

  <?php print $styles; ?>
  <?php print $scripts; ?>
</head>

Failing to output these will break many modules, and make Drupal Core to fallback to non-javascript behavior, and you don't want this to happen.

share|improve this answer
 
That won't work (neither will drupal_add_css()) as OP is replacing the the html template. The template needs to be preprocessed, a $styles var instantiated via drupal_get_css(), then print $styles added to the <head> of the page to use any of the in-built CSS-adding methods –  Clive Aug 20 at 10:26
 
@Clive I somehow missed that he does not print $styles;. Updating my answer. Again, some things are so obvious now, it's easy to forget they are not obvious for everyone. Sorry. –  Mołot Aug 20 at 10:29
 
Do I have to print the $styles in my custom .tpl file? Or the general html.tpl? Because if insert it into my custom .tpl, I get a Undefined variable: styles-error... –  Michiel Aug 20 at 10:41
 
@Michiel You are supposed to print <head> only in html.tpl.php, no matter if it's module or theme provided. And every time you do it, be sure to print them $styles and $scripts, unless you know enough to be sure you don't want to. –  Mołot Aug 20 at 10:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.