使用主题功能在身体标签之后插入代码 [英] Insert code right after body tag using theme functions
问题描述
我正尝试在Drupal网站的每个页面的开头添加一段代码。
由于我有多个页面
模板,我想以编程方式执行...但不成功。
我还是新的,虽然我得到了钩子,主题功能的要点,但是我无法想到实现这一点的正确方法。
到目前为止,我已经覆盖了 theme_preprocess_page(& $ vars)
添加必要的css和js:
pre>
函数mytheme_preprocess_page(& $ vars){
if(condition){
drupal_add_js(drupal_get_path('module','mymodule')' JS / modal.js');
}
}
如何在每个drupal页面中添加html代码,最好刚刚开始 body
标签或任何其他起始部分,通过 template.php
文件中的函数?
谢谢
在您的预处理功能中, ,将在您的page.tpl.php文件中可用。
function mytheme_preprocess_page(& $ vars){
if(condition){
$ vars ['foo'] ='< div id =bar> TEST< / div>
}
}
然后在您的页面模板中:
< body>
<?php print!empty($ foo)? $ foo:''; ?>
...
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
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 : ''; ?>
...
这篇关于使用主题功能在身体标签之后插入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!