Search code examples
drupal-7drupal-theming

Drupal 7 custom module load css for specific theme template


I have a custom module that i've created and I have set the theme template like this:

/**
 * Implemtnation of hook_theme()
 */
function custom_slider_theme($existing, $type, $theme, $path) {
    return array(
        'custom_slider' => array(
            'variables' => array('nodes' => NULL),
            'template' => 'custom_slider',
        ),
    );
}

This is all working fine.

What I can't find is how to load a css file for that template so when custom_slider.tpl.php is loaded the relevent css file is loaded. I don't need that css loaded on every page. I would rather call drupal_add_css() only when it's needed.

Any help with this is very much appreciated.

C


Solution

  • You could add it in a preprocess function, e.g.

    function custom_slider_preprocess_custom_slider(&$vars) {
      drupal_add_css(drupal_get_path('module', 'custom_slider') . '/file.css');
    }