I am theming a particular view "test" so my theme file is "views-view-table--test.tpl.php". Its working fine.

I want to add css and javascript to the "views-view-table--test.tpl.php" file Used the below code to add css and javascript. but its not working:

drupal_add_css("themes/bartik/style-test.css", $type = 'file', $media = 'all', $preprocess = FALSE);
drupal_add_js("jquery.min.js", $type = 'file', $media = 'all', $preprocess = FALSE); 

How to add css and js to views-view-table--viewname.tpl.php file.

  • Are you putting that code in the template? – Alex Gill Oct 29 '12 at 12:33
  • @Alex Gill: I am putting this code in views-view-table--test.tpl.php file. I have place this file in folder themes/barthik/views/views-view-table--test.tpl.php – Cindrella Oct 29 '12 at 12:36
  • See answer below... – Alex Gill Oct 29 '12 at 12:37

You could do this by using a process function at theme level...

function YOURTHEME_preprocess_views_view(&$vars) {
    if (isset($vars['view']->name) && $vars['view']->name == 'YOURVIEW') {
        drupal_add_css("themes/bartik/style-test.css", $type = 'file', $media = 'all', $preprocess = FALSE);
        drupal_add_js("jquery.min.js", $type = 'file', $media = 'all', $preprocess = FALSE); 
  1. Change 'YOURTHEME' to the theme you are using so if 'bartik' then you want bartik_preprocess_views_view().

  2. Change 'YOURVIEW' to the name of your view. You can get this by printing $vars['view']->name from the function.

  3. Clear caches after adding function.

  • I have added this code to bartik template.php file. But its css and js files are not loading in my view page – Cindrella Oct 29 '12 at 12:42
  • See updated answer... – Alex Gill Oct 29 '12 at 12:47
  • Did the things mentioned by you while placing the code itself. But not working – Cindrella Oct 29 '12 at 12:48
  • Mentioned the wrong css path. Thanks its working now. – Cindrella Oct 29 '12 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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