开发者

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

I'm trying to make one content type that can reuse a jQuery-gallery with different styles depending on what page you're on.

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.

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.

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?

The field only contains pure CSS that now is printed as inline-code.

EDIT: Clive post works flawlessly. Just don't forget to fix the field theming so you don't get a div in the css-section.


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']);
  } 
}

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.

Hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜