How do I programmatically render a node's teaser using the theme layer?
1 Answer
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.
-
1Isn't node_theme() what lets the theme system use a .tpl.php file for the element and also allow preprocess functions?– mpdonadio ♦Commented Dec 12, 2012 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? Commented Dec 12, 2012 at 19:10
-
1Upon 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 :) Commented Dec 12, 2012 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 arender element
instead ofvariables
.– Clive ♦Commented Dec 12, 2012 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 calledrender element
s. Thehook_theme()
docs explain it pretty well, it's not 100% intuitive but once you know, it's really useful. Usingnode_view()
will never bypass the theme layer so you should be fine :)– Clive ♦Commented Dec 12, 2012 at 19:23