render

7 common.inc render(&$element)
8 common.inc render(&$element)

Renders an element.

This function renders an element using drupal_render(). The top level element is shown with show() before rendering, so it will always be rendered even if hide() had been previously used on it.

Parameters

$element: The element to be rendered.

Return value

The rendered element.

See also

drupal_render()

show()

hide()

5 calls to render()

4 string references to 'render'

File

includes/common.inc, line 5863
Common functions that many Drupal modules will need to reference.

Code

function render(&$element) {
  if (is_array($element)) {
    show($element);
    return drupal_render($element);
  }
  else {
    // Safe-guard for inappropriate use of render() on flat variables: return
    // the variable as-is.
    return $element;
  }
}

Comments

A smarter render function…

I created this for the NineSixty theme but I think this can be very useful to all themers. Takes a simple selector and searches the render array and prints it out.

https://gist.github.com/812873

Now in dvessel's sandbox:

Now in dvessel's sandbox: http://drupal.org/sandbox/dvessel/1122312

What is this new render technique?

Read more here: Render Arrays in Drupal 7 (Documentation)

There seems to be a problem

There seems to be a problem with this. Bartik, Garland and other bundled themes blow up on render if it is called from theme('page', $output) where $output is some plain HTML you have to craft yourself (eg. when you have to IFRAME a page into a Drupal site). PHP throws an "Only variables can be passed by reference" fatal error and the execution will not reach inside the function, to the conditional in the first place. A workaround is to create a surrogate form with a single #markup inside but is this working as designed?

render an empty region

I have noticed in D7.8 that if you render an empty region. the region didn't remain empty anymore. render populates the #printed true and #children with an empty array.
I haven't found anything regarding this in any documentation
so

<?php
if(page['sidebar_first'])
print
render(page['sidebar_first']);
?>

Form submit handler not being called

You may chose to render form elements individually in your custom theme. In that case you need to remember to add the following elements to your output:

$form['form_build_id'], $form['form_id'] and $form['form_token']

If you do not include them Drupal will ignore the form submission (and your submit handler(s).

more info here.

Login or register to post comments