Render arrays overview
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
"Render Arrays" or "Renderable Arrays" are the building blocks of a Drupal page, starting with Drupal 7. A render array is an associative array which conforms to the standards and data structures used in Drupal's theme rendering system.
In many cases, the data used to build a page (and all parts of it) are kept as structured arrays until the rendering stage in the theming system. This allows enormous flexibility in changing the layout or content of a page, and provides future performance enhancements as well.
Note: While render arrays and arrays used by the Form API share elements, properties and structure, many properties on form elements only have meaning for the Form API, not for the Render API. Form API arrays are transformed into Render arrays by drupal_get_form. Passing an unprocessed Form API array to the Render API may yield unexpected results.
What is "rendering"?
Rendering in the Drupal world means turning structured "render" arrays into HTML.
What is a render array?
A render array is a classic Drupal structured array that provides data (probably nested) along with hints as to how it should be rendered (properties, like #type
). A page array might look like this:
$page = array(
'#show_messages' => TRUE,
'#theme' => 'page',
'#type' => 'page',
'content' => array(
'system_main' => array(...),
'another_block' => array(...),
'#sorted' => TRUE,
),
'sidebar_first' => array(
...
),
'footer' => array(
...
),
...
);
Why was this done?
Before Drupal 7, we could alter things like forms (with hook_form_alter()), but so many other things that needed to be altered by a module or a theme had already been rendered into HTML before any rational thing could be done with them.
In Drupal 7 and later, a module or a theme can use hook_page_alter() to change the layout or content of the page at the very last moment. This means that a tidbit of PHP code in a theme can put that block over on the right side on one page when the customer needs it there.
Altering
Both blocks and pages can be altered just as forms have been alterable for some time. Many other types are also alterable. With hook_page_alter() both modules and themes can do things like this:
function mymodule_page_alter(&$page) {
// Move search form into the footer.
$page['footer']['search_form'] = $page['sidebar_first']['search_form'];
unset($page['sidebar_first']['search_form']);
// Remove the "powered by Drupal" block
unset($page['footer']['system_powered-by']);
}
How are Render Arrays Related to Elements?
Modules have the capability of defining "elements", which are essentially prepackaged default render arrays. They use hook_element_info() to do this. An element is essentially a prepackaged render array that has a #type
and some other properties. When #type
is declared for a render array, the default properties of the element type named are loaded through the use of hook_element_info().
Creating Content As Render Array
Unlike in past versions of Drupal, almost any time a module creates content, it should be in the form of a render array. A page callback should return a render array, as should hook_block_view()'s $block['content']
. This allows your module and other modules to treat the content as data for as long as possible in the page generation process.
So a page callback might return this instead of the Drupal 6 method of rendering the data and gathering it as HTML:
function mymodule_menu() {
$items['mypage-html'] = array(
'title' => 'My page with HTML-style function',
'page callback' => 'mymodule_html_page',
'access callback' => TRUE,
);
$items['mypage-ra'] = array(
'title' => 'My page with render array function',
'page callback' => 'mymodule_ra_page',
'access callback' => TRUE,
);
return $items;
}
// Previous method (still works) of generating a page by returning HTML
function mymodule_html_page() {
$output = '<p>A paragraph about some stuff…</p>';
$output .= '<ul><li>first item</li><li>second item</li><li>third item</li></ul>';
return $output;
}
// New method of generating the render array and returning that.
function mymodule_ra_page() {
$output = array(
'first_para' => array(
'#type' => 'markup',
'#markup' => '<p>A paragraph about some stuff…</p>',
),
'second_para' => array(
'#items' => array('first item', 'second item', 'third item'),
'#theme' => 'item_list',
),
);
return $output;
}
Examples of Specific Array Types
As in the past, every Drupal "element" (see hook_element_info(), which was hook_elements() in Drupal 6) is a type. So anything that core exposes as an element or that an installed module exposes is available. Looking through system_element_info() we see a pile of predefined #types
, including page
, form
, html_tag
, value
, markup
, link
, fieldset
and many more. By convention, the #
-properties used by these #types
are documented with the respective theme function. So you can find out the properties used by #type => 'html_tag'
elements by checking out the documentation for theme_html_tag(). You can also create types and properties on the fly.
Here are three examples pulled from the Examples Project's Render Example.
$demos = array(
t('Super simple #markup') => array(
'#markup' => t('Some basic text in a #markup (shows basic markup and how it is rendered)'),
),
'prefix_suffix' => array(
'#markup' => t('This one adds a prefix and suffix, which put a div around the item'),
'#prefix' => '(prefix)',
'#suffix' => '(suffix)',
),
'theme for an element' => array(
'child' => array(
t('This is some text that should be put together'),
t('This is some more text that we need'),
),
'#separator' => ' | ', // Made up for this theme function.
'#theme' => 'render_example_aggregate',
),
);
A Sampling of Properties
Many, many properties can be applied in a given render array, and they can be created as needed. This will attempt to cover some of the most common.
Note that many of these properties are documented in the Form API Reference because the Form API has always used Render API properties, but they've traditionally not been documented as Render API properties, which they clearly are now in Drupal 7.
Property | Description |
---|---|
#type |
The Element type. If this array is an element, this will cause the default element properties to be loaded, so in many ways this is shorthand for a set of predefined properties which will have been arranged through hook_element_info(). |
#markup |
The simplest property, this simply provides a markup string for #type => 'markup' |
#prefix /#suffix |
A string to be prefixed or suffixed to the element being rendered |
#pre_render |
An array of functions which may alter the actual render array before it is rendered. They can rearrange, remove parts, set #printed = TRUE to prevent further rendering, etc. |
#post_render |
An array of functions which may operate on the rendered HTML after rendering. A #post_render function receives both the rendered HTML and the render array from which it was rendered, and can use those to change the rendered HTML (it could add to it, etc.). This is in many ways the same as #theme_wrappers except that the theming subsystem is not used. |
#theme |
A single theme hook which will take full responsibility for rendering this array element, including its children. It has predetermined knowledge of the structure of the element. Note: #theme in Drupal 7 and #theme in Drupal 6 are not really related. If you just stop thinking about Drupal 6 here, you will have an easier time.
Basically, the There is a list of all the default theme hooks at http://api.drupal.org/api/drupal/modules--system--theme.api.php/group/themeable/7 |
#theme_wrappers |
An array of theme hooks which will get the chance to add to the rendering after children have been rendered and placed into #children . This is typically used to add HTML wrappers around rendered children, and is commonly used when the children are being rendered recursively using their own theming information. It is rare to use it with #theme . |
#cache |
Mark the array as cacheable and determine its expiration time, etc. Once the given render array has been rendered, it will not be rendered again until the cache expires. Caching uses standard Drupal cache_get() and cache_set() techniques. This is an array of
Note that items marked with |
#weight |
Set a weight to the child. Children will be automatically sorted on rendering, with the function drupal_sort_weight() . |
Since every element type can declare its own properties, there are many more. Many of these (often specific to a particular element type) are described on the Form API Reference handbook page.
Resources
- Franz Heinzmann's session on the Render API in Drupal 7 at Drupalcon Chicago
- Moshe Weitzman's Drupalcon San Francisco Presentation
- The drupal_render() code.
- The Render Example module in the Examples Project is a great way to explore render arrays. The source code demonstrates how to use them and allows you to explore the actual arrays describing any page.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion