The Render API in Drupal 7 Franz Heinzmann Frando http://drupal.org/user/21850 twitter.com/frandoh
The Render API in Drupal 7 Overview What is the Render API Render API and Forms API Render arrays – A closer look The page rendering process Attaching resources Render caching Rendering into other formats Render API for themers
What is the Render API A system to render structured arrays into  a flat string of text
What is the Render API A system to render structured arrays into  a flat string of text A system to create the final page output, usually HTML, from structured arrays of data.
What is the Render API A system to render structured arrays into  a flat string of text A system to create the final page output, usually HTML, from structured arrays of data. A successor to the Form API rendering system
An example
Advantages Data can be accessed and altered later on by other modules Better reusability: Makes it much easier to use a page or block callback for more things (e.g. AJAX) One consistent system for all output generation
Render API and Forms API Render API is the improved version of the Forms API rendering system. Its center is the drupal_render() function. Forms API uses the Render API to render forms. In D7, drupal_render() is used to render nearly all page output.
Render Arrays – A closer look #type Predefine elements in hook_element_info() #access #theme and #theme_wrappers #pre_render #attached
Render Arrays – Theming #theme: name of theme function to invoke Has to render all children! In hook_theme(): Choose theming method: Element-As-Variable theming Properties-As-Variable theming
Render Arrays – Theming #theme_wrappers: Array of theme functions Wrap markup around the themed output Has to include the themed output $element['#children'] Used e.g. for forms, containers, fieldsets Allows to set #theme functions on specific elements without having to worry about surrounding markup
Render Arrays – Theming
Render Arrays – Theming
drupal_render() Takes one argument only: A render array.
drupal_render() Check #access and #printed Check if cached version is available Load defaults (if #type is set) Call #pre_render callbacks Call #theme or concenate children Call #theme_wrappers Call #post_render Handle #attached (JavaScript, CSS, Libraries) If #cache is set: Cache the output string Return #prefix . $output . #suffix
The page rendering process All page elements are assembled into one single render array. This includes:  main content (page callback result) blocks anything else added by modules Let's look how it actually works.
The page rendering process hook_page_build() -> Add elements hook_page_alter() -> Alter existing elements block.module is no longer required!
Render caching The #cache property Cache resource intensive elements while other parts are created dynamically
Attached resources The #attached property Attach CSS, JavaScript and Libraries to elements instead of adding them in hook_init()
Attached resources The #attached property Attach CSS, JavaScript and Libraries to elements instead of adding them in hook_init() Makes AJAX/AHAH loading of individual elements possible Makes caching individual elements possible without „losing“ needed resources
Attached resources
Render caching The #cache property Cache resource intensive elements while other parts are created dynamically Cache by permission, role, user, … Cache by an SQL query: Compatible with node access!
Render caching The #cache property Cache resource intensive elements while other parts are created dynamically Cache by permission, role, user, … Cache by an SQL query: Compatible with node access! Edge Side Includes (ESI)
Render caching
Taking over page rendering & rendering into other formats Not yet much used, but has been a design goal of the D7 Render API Delivery callbacks hook_menu_alter() hook_page_delivery_callback_alter() Changing $page['#theme']
The Render API for themers $element in theme functions and templates render(), show(), hide() Using hook_page_alter() in a theme: Change theme functions for specific instances of a theme hook!
Summary Use render arrays in your module's page and block callbacks! Do not call theme()! Use #attached, #pre_render, #cache to easily make your output re-usable and fast Use the power of render arrays to avoid complicated code in theme
Challenges ahead Build and establish shared library of easily reusable interface elements Make it easier to render render arrays into other formats D8: Settle on one method of element theming? D8: Actually use render arrays through all of core – no calls to theme()!
Resources API documentation:  http://api.drupal.org Render API doc on drupal.org: http://drupal.org/node/930760 Examples module:  http://drupal.org/project/examples
What did you think? Locate this session on the DCC website: http://chicago2011.drupal.org/sessions Click the “Take the Survey” link. Thanks!

The Render API in Drupal 7

  • 1.
    The Render APIin Drupal 7 Franz Heinzmann Frando http://drupal.org/user/21850 twitter.com/frandoh
  • 2.
    The Render APIin Drupal 7 Overview What is the Render API Render API and Forms API Render arrays – A closer look The page rendering process Attaching resources Render caching Rendering into other formats Render API for themers
  • 3.
    What is theRender API A system to render structured arrays into a flat string of text
  • 4.
    What is theRender API A system to render structured arrays into a flat string of text A system to create the final page output, usually HTML, from structured arrays of data.
  • 5.
    What is theRender API A system to render structured arrays into a flat string of text A system to create the final page output, usually HTML, from structured arrays of data. A successor to the Form API rendering system
  • 6.
  • 7.
    Advantages Data canbe accessed and altered later on by other modules Better reusability: Makes it much easier to use a page or block callback for more things (e.g. AJAX) One consistent system for all output generation
  • 8.
    Render API andForms API Render API is the improved version of the Forms API rendering system. Its center is the drupal_render() function. Forms API uses the Render API to render forms. In D7, drupal_render() is used to render nearly all page output.
  • 9.
    Render Arrays –A closer look #type Predefine elements in hook_element_info() #access #theme and #theme_wrappers #pre_render #attached
  • 10.
    Render Arrays –Theming #theme: name of theme function to invoke Has to render all children! In hook_theme(): Choose theming method: Element-As-Variable theming Properties-As-Variable theming
  • 11.
    Render Arrays –Theming #theme_wrappers: Array of theme functions Wrap markup around the themed output Has to include the themed output $element['#children'] Used e.g. for forms, containers, fieldsets Allows to set #theme functions on specific elements without having to worry about surrounding markup
  • 12.
  • 13.
  • 14.
    drupal_render() Takes oneargument only: A render array.
  • 15.
    drupal_render() Check #accessand #printed Check if cached version is available Load defaults (if #type is set) Call #pre_render callbacks Call #theme or concenate children Call #theme_wrappers Call #post_render Handle #attached (JavaScript, CSS, Libraries) If #cache is set: Cache the output string Return #prefix . $output . #suffix
  • 16.
    The page renderingprocess All page elements are assembled into one single render array. This includes: main content (page callback result) blocks anything else added by modules Let's look how it actually works.
  • 17.
    The page renderingprocess hook_page_build() -> Add elements hook_page_alter() -> Alter existing elements block.module is no longer required!
  • 18.
    Render caching The#cache property Cache resource intensive elements while other parts are created dynamically
  • 19.
    Attached resources The#attached property Attach CSS, JavaScript and Libraries to elements instead of adding them in hook_init()
  • 20.
    Attached resources The#attached property Attach CSS, JavaScript and Libraries to elements instead of adding them in hook_init() Makes AJAX/AHAH loading of individual elements possible Makes caching individual elements possible without „losing“ needed resources
  • 21.
  • 22.
    Render caching The#cache property Cache resource intensive elements while other parts are created dynamically Cache by permission, role, user, … Cache by an SQL query: Compatible with node access!
  • 23.
    Render caching The#cache property Cache resource intensive elements while other parts are created dynamically Cache by permission, role, user, … Cache by an SQL query: Compatible with node access! Edge Side Includes (ESI)
  • 24.
  • 25.
    Taking over pagerendering & rendering into other formats Not yet much used, but has been a design goal of the D7 Render API Delivery callbacks hook_menu_alter() hook_page_delivery_callback_alter() Changing $page['#theme']
  • 26.
    The Render APIfor themers $element in theme functions and templates render(), show(), hide() Using hook_page_alter() in a theme: Change theme functions for specific instances of a theme hook!
  • 27.
    Summary Use renderarrays in your module's page and block callbacks! Do not call theme()! Use #attached, #pre_render, #cache to easily make your output re-usable and fast Use the power of render arrays to avoid complicated code in theme
  • 28.
    Challenges ahead Buildand establish shared library of easily reusable interface elements Make it easier to render render arrays into other formats D8: Settle on one method of element theming? D8: Actually use render arrays through all of core – no calls to theme()!
  • 29.
    Resources API documentation: http://api.drupal.org Render API doc on drupal.org: http://drupal.org/node/930760 Examples module: http://drupal.org/project/examples
  • 30.
    What did youthink? Locate this session on the DCC website: http://chicago2011.drupal.org/sessions Click the “Take the Survey” link. Thanks!