Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 412 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I decide between theme('node', $node) and drupal_render($node->content) for programmatic $node output

#1
Given a `$node`, I'm trying to decide between the following two ways of outputting that $node.

**either**


$output = theme('node', $node);

**or**

node_build_content($node);
$output = drupal_render($node->content);

They both seem to give similar results, but is there something I should consider before I choose one way over the other?
Reply

#2
Your output is similar if there are no other modules and themes altering the output via the theme layer.

But! If you bypass the theme layer, you'll probably start experiencing unexpected behaviour when you install modules or themes and change config settings that use the theme layer to alter the node's output.

In short, by bypassing the theme layer, you're building error into your application. These error are likely to occur after you hand your application over to someone (a client perhaps) who starts changing settings in admin/

See the decorator pattern if you're interested. Drupal uses this extensively.

[To see links please register here]






Reply

#3
Well, sortof.

What you really want is

$output = node_view($node);

if you just call node_build_content, and then call theme('node', $node), `hook_nodeapi('alter')` is never called, nor is `hook_link()`.

So if any module is expecting to alter the built node, it wont get a chance to, and if there is supposed to be links on it they wont be there either.

Additionally you can call `$output = node_view($node, FALSE, FALSE, FALSE);` which gives you the node without the links.

See the documentation for [node_view()][1].


[1]:

[To see links please register here]

Reply

#4
If you look into the source of `drupal_render()`, it actually calls `theme()` itself as well (provided a `#theme` override is provided).
Reply

#5
The solution that worked here was a combination of both of these techniques. Just using `theme('node', $node);` doesn't seem to work without the help of `node_build_content($node);`.

Here is the result of an example only using `theme('node', $node);`.

![alt text][1]


But if we first do `node_build_content($node);` prior to handling the $node to the theme function, you can see that the form button is also rendered.

![alt text][2]

**Therefore, the real solution is:**

node_build_content($node);
$output = theme('node', $node);



[1]:

[2]:
Reply

#6
First is recommended, as it is passed via the theme layer.
Reply

#7
In Drupal 7, you should do :

$elements = node_view($node, 'teaser');
$rendered_node = drupal_render($elements);
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through