0

I have created a custom module where I have define my page url's & contents are created through my custom tpl files.

I am applying some css & js on those custom contents/pages/tpl files.

What I want : I don't want to add path of those css & js in all pages. I want to define those css & js in separate file & I want to call that css & js in my custom pages through variable. Simply I want to create a header file which hold my css & js. And I want to call that header file in my custom pages.

Why I want to do this : Right now the path of the css & js is not fixed. So if am I add drupal_add_css() & drupal_add_js() in every tpl file then will change the path in every tpl file. So I want to create a header file where am I put the css & js path & if the path will change then I have to change the path in only header file.

How can I implement this?

4
  • From the visitor's point of view it's actually better to have the same CSS and JS on each page, even if that adds weight to the page. See this answer: stackoverflow.com/questions/1813383/… Jan 27, 2015 at 15:39
  • Please check my updated question @AlfredArmstrong
    – Nishant
    Jan 28, 2015 at 6:12
  • "the path of the css & js is not fixed", how ? These files are not moving in your file system between two server requests.
    – B2F
    Jan 28, 2015 at 9:46
  • Not to be overly harsh, but I think you are missing an understanding of how the module and theme interact, and how Drupal renders the requested page. Jan 28, 2015 at 17:23

3 Answers 3

1

If you do not want, or are unable, to modify the theme html or page templates, you can also use drupal_add_css and drupal_add_js in the page_callback function defined in your modules menu array youmodule_menu().

If you have access to the template.php of the theme, you can use the preprocess hook to assign a variable to the file name, then do an include in the html template. Or from within your module use drupal_add_html_head, but this has limitations.

Using drupal_add_(css|js) the links go into the head element of those pages by default before the themeing phase begins, but you can also specify the boostrap phase and location.

EDIT

Using drupal_get_path from within yourmodule_init hook or the page_callback, and with the css and js stored within your module structure, you can then add the css and js using the drupal_add functions. This makes a single place you need to maintain the 'paths' to the files.

1
  • Please check my updated question.
    – Nishant
    Jan 28, 2015 at 6:00
0

Rupesh is right, actually drupal_add_css and drupal_add_js are adding your files to the header of the website.

You don't have to call these in every tpl file, that is what preprocess functions are made for.

You could, for instance, implement a hook function "yourtheme_preprocess_page(&$variables)" (or _preprocess_node, or any other suitable preprocess) in your theme's template.php, then add your js with druapl_add_js.

If you have trouble with theme's preprocess function, my advice is to try the Zen theme which has these functions as commented placeholders.

If needed, you should be able to filter templates names with the preprocess &$variables parameter.

1
  • Please check my updated question.
    – Nishant
    Jan 28, 2015 at 6:02
0

Use drupal_add_css and drupal_add_js function on that desired tpl to add css and js.

You can use CSS Injector and JS injector module. These module also support placement option to define on page or callback.

4
  • Thanks @Rupesh. Through this way I have to add "drupal_add_css" & path inside function in my every custom tpl file. But my query is that I want to put those js & css in another file separately & I want to call those through variable.
    – Nishant
    Jan 27, 2015 at 13:03
  • Why you not call as a function? make a function and add all necessary css, js. call it. Simple
    – Rupesh
    Jan 27, 2015 at 13:09
  • There are lots of my custom tpl files. And I don't want to call as a function in every tpl file. Creating of "header" is the best way to store our css & js inside them & call that header in other files.
    – Nishant
    Jan 27, 2015 at 13:11
  • Please check my updated question.
    – Nishant
    Jan 28, 2015 at 6:02

Your Answer

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

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