drupal_add_css,drupal_add_js不起作用 [英] drupal_add_css, drupal_add_js does not work
问题描述
我正在使用 drupal_add_css()
和 drupal_add_js()
将CSS和JavaScript文件添加到我的Drupal站点。我在一个名为control的模块中这样做,所以我使用的函数称为 control_preprocess_page(& $ vars)
。
I am using drupal_add_css()
and drupal_add_js()
to add CSS and JavaScript files to my Drupal site. I am doing this in a module called control so the function I'm using is called control_preprocess_page(&$vars)
.
但是在我的主题中没有添加任何内容!
But in my theme nothing is added!
推荐答案
这些函数在preprocess_page()功能是 template_preprocess_page()(其中被称为第一)已经将结构化内容格式化为变量$ scripts和$ styles。如果要在预处理级别添加其他js或css,则需要重新生成2个变量,如下所示:
The reason these functions aren't working within the preprocess_page() function is that template_preprocess_page() (which is called first) has already formatted the structured content into variables $scripts and $styles. If you want to add additional js or css at the preprocess level, you need to regenerate those 2 variables, something like this:
function control_preprocess_page(&$vars) {
// Add new CSS.
drupal_add_css('path/to/css/foo.css');
// Rebuild the 'styles variable.
$vars['styles'] = drupal_get_css();
// Add new JS.
drupal_add_js(...);
$vars['scripts'] = drupal_get_js();
}
在hook_init中使用drupal_add_js / drupal_add_css或更准确的目标函数(例如,一个更改钩子或nodeapi钩子(如果适用))将避免重新生成那些变量。
Using drupal_add_js/drupal_add_css in hook_init, or a more precisely targeted function (eg, an alter hook, or nodeapi hook, if applicable), will avoid having to regenerate those variables.
这篇关于drupal_add_css,drupal_add_js不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!