2011-02-26 61 views
4

我想爲視圖添加一個jquery插件。但是,我不想通過.info文件添加,因爲我不希望每個頁面都有這個文件。我也不想將它添加到視圖的標題或創建一個view.tpl.php文件。Drupal 6 - 將Javascript添加到視圖

This page shows how to add Java Script to a node via template.php。無論如何要做類似的事情來通過template.php文件添加Java腳本到視圖?

回答

5

這裏是丹尼爾·魏納的回答的例子。它假設您的視圖被稱爲「博客」,並且您的顯示ID爲「page_1」。這段代碼進入template.php。請記住在向template.php添加新函數後清除模板緩存。

/** 
* Implements hook_preprocess_views_view(). 
*/ 
function THEMENAME_preprocess_views_view(&$vars) { 
    $view = $vars['view']; 
    // Load the blog.js javascript file when showing the Blog view's page display. 
    if ($view->name == 'blog' && $view->current_display == 'page_1') { 
    global $theme; 
    drupal_add_js(drupal_get_path('theme', $theme) . '/js/blog.js', 'theme', 'footer', FALSE, TRUE, FALSE); 
    } 
} 
0

可以使用鉤hook_preprocess_views_view

+0

嗨你有一個這樣如何工作的例子。 (即代碼我將放在template.php文件中的一個視圖稱爲「示例」)謝謝! – 2011-02-28 22:03:20