在 Drupal 7 中将字段内容注入 html.tpl.php
我正在尝试制作一种内容类型,它可以根据您所在的页面重用具有不同样式的 jQuery 库.
I'm trying to make one content type that can reuse a jQuery-gallery with different styles depending on what page you're on.
所以我在放置 CSS 的地方创建了一个名为 field_CSS 的字段.但是出于性能原因(以及为了清理代码)我想把它放在 HEAD 部分.
So i made one field called field_CSS where I put the CSS. However for performance reasons (and for the sake of cleaning up the code) I want to put it in the HEAD-section.
head 部分的内容在 html.php.tpl-filde 中呈现,该字段在特定节点的内容类型中.
The content in the head section is rendered in the html.php.tpl-filde and the field is in the content type of the specific node.
我试过 <?php print render($content['field_CSS']) ?><?php 打印渲染($page['field_CSS']) ?><?php print $node->field_CSS[0]['view'];?>
和许多其他变体.谁知道要写什么才能让它出现在 html.tpl.php 文件中?
I've tried <?php print render($content['field_CSS']) ?>
<?php print render($page['field_CSS']) ?>
<?php print $node->field_CSS[0]['view']; ?>
and a lot of other variants. Anyone who knows what to write to make it show up in the html.tpl.php file?
该字段仅包含现在打印为内联代码的纯 CSS.
The field only contains pure CSS that now is printed as inline-code.
Clive 帖子完美无缺.只是不要忘记修复字段主题,这样您就不会在 css 部分中获得 div.
Clive post works flawlessly. Just don't forget to fix the field theming so you don't get a div in the css-section.
推荐答案
该节点通常在 html.tpl.php 中不可用,因此您需要在预处理函数中手动获取字段内容.在你的主题模板文件中加入这样的内容:
The node is not usually available in html.tpl.php so you'll need to get your field content manually in a preprocess function. Put something like this in your theme's template file:
function MYTHEME_preprocess_html(&$vars) {
$node = menu_get_object();
if ($node && isset($node->nid)) {
$node = node_load($node->nid);
node_build_content($node);
$vars['extra_css'] = render($node->content['field_CSS']);
}
}
然后,您将在 html.tpl.php 中拥有变量 $extra_css
,该变量将包含您呈现的字段.实现预处理功能后,您需要刷新缓存并将 MYTHEME
替换为您的主题名称.
Then you'll have the variable $extra_css
in html.tpl.php which will contain your rendered field. You'll need to flush the caches once you've implemented the preprocess function and replace MYTHEME
with the name of your theme.
希望有帮助
相关文章