1

I have a node type with a body, that I want to display in a custom node template. When I use code like

print render($content['body']);

I get a mess of div tags like

<div class="field field-name-body field-type-text-with-summary field-label-hidden">
    <div class="field-items">
        <div class="field-item even">Here is my body content</div>
    </div>
</div>

I appreciate Drupal's rich markup, but I just want

 <p>Here is my body content</p>

According to http://api.drupal.org/api/drupal/modules!field!field.module/function/field_view_field/7 I should be using render(), but it render() doesn't seem to be giving me the control I want. Is there a best-practice way to do this?

3

The extra junk is added by the field.tpl.php file from core. But hey you can customize it.

Create a new file in your theme's templates folder. (For available variables, see here)

File name: field--body.tpl.php (See http://drupal.org/node/1089656 for template file suggestions) Contents:

Now, when you use render() in your node.tpl.php files, you will see the p tag wrapped content instead of the extra junk.

Update: I have forgotten that the $item comes from $items array. Updated the code above. If the body field has multiple instances, each one will be shown in individual p tags.

3
  • I think I will go this route, but does this method affect performance greatly? – JohnWoltman Aug 7 '12 at 15:47
  • @JohnWoltman As a suggestion (dunno if it's too late for you) if you don't like 'heavy' markup and you're considering doing it at the theme level, it might be worth choosing an existing minimal theme (I'm afraid I can't recommend any in particular) and subtheme that. If you don't like the field template's markup then I imagine there's gonna be a whole lot of other templates you won't like :) – Andy Aug 7 '12 at 17:00
  • Very good advice Andy, but as you feared it is too late for me :) – JohnWoltman Aug 8 '12 at 14:41
1

I'd recommend using Display Suite with the Extras sub-module enabled, which gives a fair amount of control over each field. The screenshot shows the expert settings, but it also provides a full reset template, a minimal template, and allows you to use the default if you want.

screenshot of settings

0

If in your template file you do this:

<?php
print_r($node);
?>

Note: you can remove the code mention above when you are done getting your information.

You will see all the parts of the node object and from there you can just pick:

$node->body['und'][0]['value']

But to avoid issues with the language or translations I recommend you us it like this:

$node->body[$node->language][0]['value']

so at the end if you just want the content wrap in paragraph tags it will be something like this:

<p><?php print $node->body[$node->language][0]['value']; ?></p>

Thank you.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.