18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3<br />

By the way, does your brain hurt yet? You may want to take a break now; go out and<br />

get some air, or, at the very least, have a strong drink handy when you start reading<br />

the next section.<br />

Render elements<br />

Render elements are new to <strong>Drupal</strong> 7's theme layer. They've existed since <strong>Drupal</strong> 4.7<br />

as part of the Form API, but they've now been injected into the heart of the theme<br />

system. A Render element is a complex data structure passed as a single parameter<br />

to theme(), as one of its variables. Render elements are fundamentally nested arrays<br />

that can include:<br />

• The data to be rendered<br />

• Other render elements which are considered "children" of the element<br />

• An array of structures such as CSS and JavaScript files, that should be<br />

attached to the page when it is rendered<br />

• A list of theme hooks that can be used to theme the data<br />

• A list of callback functions to run on the element before and after it<br />

is themed<br />

In template files, render elements are handled slightly differently then normal<br />

variables, using the syntax we saw earlier in our typical-hook.tpl.php example:<br />

<br />

In theme functions, render elements are included with its output using the<br />

drupal_render() function:<br />

$output .= drupal_render($element);<br />

Let's look at a simple render element:<br />

$element = array(<br />

'#prefix' => '',<br />

'#markup' => '' . t('There is no spoon.') . '',<br />

'#suffix' => '',<br />

);<br />

In the preceding render element our main property is the #markup property which<br />

uses a string containing HTML markup as-is for the rendered element. The other<br />

properties do exactly what they hint at, prepending or appending HTML markup<br />

to the rendered element. If drupal_render($element) was called, it would simply<br />

return the three strings concatenated together.<br />

[ 77 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!