使用主题函数在 body 标记后插入代码
我正在尝试在 Drupal 站点的每个页面的开头添加一段代码。 由于我有多个 page
模板,我想以编程方式执行此操作...但没有成功。
我还是个新手,虽然我了解了钩子、主题函数等的要点,但我只是想不出实现这一目标的正确方法。
到目前为止,我已经覆盖了 theme_preprocess_page(&$vars)
以添加必要的 css 和 js:
function mytheme_preprocess_page(&$vars) {
if(condition) {
drupal_add_js(drupal_get_path('module', 'mymodule').'/js/modal.js');
}
}
How can I now add html code in every drupal page,最好是在打开 body 之后
标签或任何其他起始部分,通过template.php
文件中的函数?
谢谢
I am trying to add a piece of code at the begining of every page in a Drupal site.
Since I have more than one page
template, I want to do this programatically... but am not succeeding.
I am still new and, though I get the gist of hooks, theme functions, and the such, I just can't figure the correct way to achieve this.
So far I've overriden the theme_preprocess_page(&$vars)
to add the necessary css and js:
function mytheme_preprocess_page(&$vars) {
if(condition) {
drupal_add_js(drupal_get_path('module', 'mymodule').'/js/modal.js');
}
}
How can I now add html code in every drupal page, preferably just after the opening body
tag or in any other starting section, via a function in the template.php
file?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在您的预处理函数中,任何变量集都将在您的 page.tpl.php 文件中可用。
然后,在您的页面模板中:
In your preprocess function, any variable set, will be available in your page.tpl.php file.
then, in your page templates:
查看https://www.drupal.org/project/google_tag。他们是这样做的:
Had a look at https://www.drupal.org/project/google_tag. This is how they did it:
这应该使您不必修改模板中的任何代码:
This should keep you from having to modify any code in your templates:
我的方法最终是覆盖页面中的第一个渲染块,在我的例子中是语言切换器。
由于我已经重写它来自定义它,所以这没什么大不了的,但无论如何这是实现这一目标的一种丑陋的方式。
由于语言切换器在每个页面中都会呈现,因此它可以工作。无论出于何种原因,当语言切换器停止显示的那一天,此解决方案将失败。
My approach finally was overriding the first rendered block in the page, in my case the Language Switcher.
Since I already was overriding it to customize it, it wasn't too much of a big deal, but it is anyway an ugly way to achieve that.
Since the Language Switcher is rendered in every page, it works. The day the language switcher stops being displayed for whatever reason, this solution will FAIL.
您可以将其添加到页脚,并将选项传递到 drupal_add_js 函数。
这将通过模板
html.tpl.php
中的$page_bottom
变量在结束正文标记之前打印。You can add it to the footer with an option passed into the drupal_add_js function.
This will end up printing just before the closing body tag via the
$page_bottom
variable in the templatehtml.tpl.php
.另一种方法是使用本机 drupal 方法
drupal_add_js
和drupal_get_js
。// 首先,添加带有自定义“范围”的 JS(在处理阶段之前)
// 丑陋的方式:将
drupal_get_js
直接添加到html.tpl.php
中(但是对于测试,很有用):// 更简洁的方法:在流程方法中使用中间变量
// 与
theme.inc
中的template_process_html
完全相同,享受吧:)
Another way to do it is to use the native drupal methods
drupal_add_js
anddrupal_get_js
.// first, add your JS with a custom "scope" (before process phase)
// ugly way : add the
drupal_get_js
directly intohtml.tpl.php
(but for testing, it's useful):// A cleaner way : use an intermediate variable, in a process method
// Exactly like
template_process_html
, intheme.inc
Enjoy :)