1

I am in a custom tpl.php file [node--netrunner-card--Operation--full.tpl.php]. This is for a custom content type [netrunner_card].

I am trying to output the contents of the $body variable and unable to do so via the prescribed avenue. I copied node.tpl.php into my theme and renamed it. I can output the contents of body but I have to do this like so:

print render($body[0]['value']);

I should be able to do it like so:

print render($body);

But the second option renders nothing.

Any ideas?

1

Actually, the documentation for node.tpl.php says, in the description for the $content variable.

An array of node items. Use render($content) to print them all, or print a subset such as render($content['field_example']). Use hide($content['field_example']) to temporarily suppress the printing of a given element.

The node body is a field, and you should use render($content['body']) to render it.

The note about field variables is the following one:

Field variables: for each field instance attached to the node a corresponding variable is defined; for example, $node->body becomes $body. When needing to access a field's raw values, developers/themers are strongly encouraged to use these variables. Otherwise they will have to explicitly specify the desired field language; for example, $node->body['en'], thus overriding any language negotiation rule that was previously applied.

You should use $body only when you need to access the field's raw values, not to render the field.

  • I've read that section a hundred times now. I think I need to go get a drink and come back. My mind is whirling with trying to both learn AND implement this stuff while doing other things as well. Maybe I've got too much multi-tasking. I will say that I feel like every post here brings me even closer to fully grasping Drupal on the level that I want to. I appreciate all the help from the drupal.stackexchange group. – randomblink May 22 '13 at 16:00
  • 1
    Sometimes, it is easy to miss a single line and not understanding what the documentation says. It happened to me in the past, and I know what scratching your head for hours means. :) – kiamlaluno May 22 '13 at 16:18
0

I decided to ignore the notes in the node.tpl.php file and output like I did for all of my other fields, treating body like any other field instead of one of the two fields auto-created for all content types.

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

This gives me the output I need!

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.