10

How do I programmatically render a node's teaser using the theme layer?

20

node_view() is the standard function for rendering nodes, e.g.

$node = node_load(1);
$view = node_view($node, 'teaser');
$rendered = drupal_render($view);

There is a node theme defined by the node module (in node_theme()) but I've never actually seen it used.

  • 1
    Isn't node_theme() what lets the theme system use a .tpl.php file for the element and also allow preprocess functions? – mpdonadio Dec 12 '12 at 18:22
  • Yeah I'm concerned about bypassing the theme layer. If I did that, then other modules wanting to chip in would not be able to. At least, I think that's correct? – Lester Peabody Dec 12 '12 at 19:10
  • 1
    Upon analyzing api.drupal.org/api/drupal/modules%21node%21node.module/function/… if you view the implementation code, where it says $build += array(, it does have a #theme attribute in there which I'm guessing tells Drupal to use the theme system. Correct me if I'm wrong. I'm just now starting to 'get' the Drupal API :) – Lester Peabody Dec 12 '12 at 19:13
  • @MPD Yeah I didn't explain that very well, I just meant I'd never seen a call like theme('node', ... before. In fact that wouldn't even work because it defines itself as having a render element instead of variables. – Clive Dec 12 '12 at 19:20
  • @Lester Spot on, there are 2 ways to call theme functions, the 'normal' way (theme('theme_name', $args) and by using so called render elements. The hook_theme() docs explain it pretty well, it's not 100% intuitive but once you know, it's really useful. Using node_view() will never bypass the theme layer so you should be fine :) – Clive Dec 12 '12 at 19:23

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.