在Drupal 7中将字段内容注入html.tpl.php [英] Injecting field content to html.tpl.php in Drupal 7

查看:137
本文介绍了在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.

所以我做了一个字段称为field_CSS,其中我放置了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.

头部分中的内容呈现在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 print render($ 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']);
  } 
}

然后你将有变量<$ c $ 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.

希望有助于

这篇关于在Drupal 7中将字段内容注入html.tpl.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆