使用主题函数在 body 标签之后插入代码 [英] Insert code right after body tag using theme functions
问题描述
我正在尝试在 Drupal 站点的每个页面的开头添加一段代码.由于我有多个 page
模板,我想以编程方式执行此操作...但没有成功.
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.
到目前为止,我已经覆盖了 theme_preprocess_page(&$vars)
以添加必要的 css 和 js:
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');
}
}
我现在如何通过 template.php
body 标记之后或在任何其他起始部分中> 档案?
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?
谢谢
推荐答案
在您的预处理函数中,任何变量集都将在您的 page.tpl.php 文件中可用.
In your preprocess function, any variable set, will be available in your page.tpl.php file.
function mytheme_preprocess_page(&$vars) {
if (condition) {
$vars['foo'] = '<div id="bar">TEST</div>';
}
}
然后,在您的页面模板中:
then, in your page templates:
<body>
<?php print !empty($foo) ? $foo : ''; ?>
...
这篇关于使用主题函数在 body 标签之后插入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!