在Drupal 7中向html.tpl.php注入字段内容


Injecting field content to html.tpl.php in Drupal 7

我正试图使一个内容类型,可以重用不同风格的jquery库,这取决于你在什么页面。

因此我创建了一个名为field_CSS的字段,用于放置CSS。但是,出于性能原因(以及为了清理代码),我想把它放在head部分。

标题部分的内容在html.php中呈现。tpl- field,该字段为特定节点的内容类型。

我试过<?php print render($content['field_CSS']) ?> <?php print render($page['field_CSS']) ?> <?php print $node->field_CSS[0]['view']; ?>和许多其他变体。谁知道如何在html.tpl.php文件中显示它?

字段只包含纯CSS,现在作为内联代码打印。

编辑:Clive post工作完美无瑕。只是不要忘记修复字段主题,这样你就不会在css-section中出现div

该节点通常在html.tpl.php中不可用,因此您需要在预处理函数中手动获取字段内容。在你的主题模板文件中添加如下内容:

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替换为主题的名称。

希望有帮助