1

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

1 Answer 1

2

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');
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thnaks @Clive - But what if I want the module to be self seficient and loads it's own resources? I know how to add it to the .info file of the module but I only want it to load when the templete used in that module is being used.
Not sure I understand - the above method is exactly how to do that unless I've missed something?
You can implement a preprocess function for a theme in the same module that defines that theme - could that be the confusion?
Sorry for the confusion. Does this function add the css to every page or just the page that uses the template custom_slider.tpl.php which is only called when the url is /mycustomslider
The CSS will only be added when theme('custom_slider') is called, so it won't be added to any page that doesn't call it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.