SlideShare a Scribd company logo
Render API Pavel Makhrinsky http://donetsk.drupal.ua
Render API Pavel Makhrinsky e-mail:  [email_address] skype: gumanista
Overview Form API successor Drupal 6 ‘theme’ replacement System to build structured arrays into content The way to build presentation layer
Theming ways Noob way Drupal 6 way Drupal 7 way
Noob way Inline Inline Inline Inline Inline 1  <div class=&quot;nav&quot;> 2  <ul class=&quot;topnav&quot;>  3  <li id=&quot;lefttopnav&quot;  <?php if ( $levelone  ==  'home' )  echo   &quot;class= \&quot; current \&quot; &quot; ; ?> > <?php echo   $menu1 ; ?> </li> 4  <li  <?php if ( $levelone  ==  &quot;item-2&quot; )  echo   &quot;class= \&quot; current \&quot; &quot; ; ?> >  <?php echo   $menu2 ; ?> </li> 5  <li  <?php if ( $levelone  ==  &quot;item-3&quot; )  echo   &quot;class= \&quot; current \&quot; &quot; ; ?> >  <?php echo   $menu4 ; ?> </li> 6  <li  <?php if ( $levelone  ==  &quot;item-4&quot; )  echo   &quot;class= \&quot; current \&quot; &quot; ; ?> >  <?php echo   $menu6 ; ?> </li> 7  <li  <?php if ( $levelone  ==  &quot;item-5&quot; )  echo   &quot;class= \&quot; current \&quot; &quot; ; ?> >  <?php echo   $menu5 ; ?> </li> 8  <li  <?php if ( $levelone  ==  'item-6' )  echo   &quot;class= \&quot; current \&quot; &quot; ; ?> >  <?php echo   $menu7 ; ?> </li> 9  </ul> 10  </div>
Drupal 6 way Use theme functions Implement hook_theme Move large markup to templates 1  $items  =  array ( 'item-1' ,  'item-2' ,  'item-3' ,  'item-4' ,  'item-5' ); 2  $output  = theme( 'item_list' ,  $items );
Drupal 6 way - advantages Common way to render elements Reusable functions Predictable markup Possibility to change generation Output altering
Drupal 6 way - disadvantages Slower performance Caching difficulties Different parameters
Drupal 7 way Use renderable arrays Alter content you need 1  $items  =  array ( 'item-1' ,  'item-2' ,  'item-3' ,  'item-4' ,  'item-5' ); 2  $output  =  array ( 3  '#items'  =>  $items , 4  '#theme'  =>  'item_list' 5  );
Drupal 7 way advantages Content alterable in a common way All the renderable elements have preprocess and process functions Transparent caching Resources could be attached to elements
Render API details hook_theme Renderable array structure Content altering #type, #theme, #theme_wrappers #states Resources Performance and caching
hook_theme() hook_theme variables | render element file path template function preprocess functions
Renderable array structure ‘ #’ elements system elements #children #access #printed #sorted
Content altering #pre_render #post_render preprocess and process  functions
#type Loads defaults from hook_element_info() 1  function  module_template_element_info() { 2  return   array ( 3  'file_template'  =>  array ( 4  '#name'  =>  'misc' , 5  '#fileinfo'  =>  array ( 6  'filename'  =>  '[module_name].[name].[extension]' , 7  'path'  =>  'includes' , 8  'extension'  =>  'inc' 9  ) 10  ) 11  );   11  } 1  // If the default values for this element have not been loaded yet, populate 2  // them. 3  if  ( isset ( $elements [ '#type' ]) &&  empty ( $elements [ '#defaults_loaded' ])) { 4  $elements  += element_info( $elements [ '#type' ]); 5  }
#theme Invokes theme() function 1  // Get the children of the element, sorted by weight. 2  $children  = element_children( $elements ,  TRUE );   3  4  // Initialize this element's #children, unless a #pre_render callback already   5  // preset #children.   6  if  (! isset ( $elements [ '#children' ])) {   7  $elements [ '#children' ] =  '' ;   8  }   9  // Call the element's #theme function if it is set. Then any children of the 10  // element have to be rendered there. 11  if  ( isset ( $elements [ '#theme' ])) { 12  $elements [ '#children' ] = theme( $elements [ '#theme' ],  $elements ); 13  } 14  // If #theme was not set and the element has children, render them now. 15  // This is the same process as drupal_render_children() but is inlined 16  // for speed. 17  if  ( $elements [ '#children' ] ==  '' ) { 18  foreach  ( $children   as   $key ) { 19  $elements [ '#children' ] .= drupal_render( $elements [ $key ]); 20  } 21  }
#theme_wrappers Wrap #children element with code 1  // Let the theme functions in #theme_wrappers add markup around the rendered 2  // children. 3  if  ( isset ( $elements [ '#theme_wrappers' ])) { 4  foreach  ( $elements [ '#theme_wrappers' ]  as   $theme_wrapper ) { 5  $elements [ '#children' ] = theme( $theme_wrapper ,  $elements ); 6  } 7  }
#states Adds JavaScript to change the state of an element based on another element 1  $form [ 'toggle_me' ] =  array (   2  '#type'  =>  'checkbox' ,   3  '#title'  => t( 'Tick this box to type' ),   4  );   5  $form [ 'settings' ] =  array (   6  '#type'  =>  'textfield' ,   7  '#states'  =>  array (   8  // Only show this field when the 'toggle_me' checkbox is enabled.   9  'visible'  =>  array ( 10  ':input[name=&quot;toggle_me&quot;]'  =>  array ( 'checked'  =>  TRUE ), 11  ), 12  ), 13  );  
Resources #attached property Allow attach CSS JavaScript Libraries Not cached
Performance and caching Setting cache for renderable arrays Some cache usage tips
#cache 'keys ' => an array of keys which will be concatenated to form the cache key. 'bin ' => the name of the cache bin to be used (as in 'cache' or 'cache_page', etc. 'expire ' => a Unix timestamp indicating the expiration time of the cache. 'granularity ' => a bitmask indicating the cache type. This should be  DRUPAL_CACHE_PER_PAGE ,  DRUPAL_CACHE_PER_ROLE , or  DRUPAL_CACHE_PER_USER
Some cache usage tips Don’t cache small items Items from #attached not stored with rendered items Use cache targeting Cache items will not be expired until cron runs, regardless of the expiration time used Elements 5 100 500 With #cache 3211 3276 3235 Without #cache 747 4257 18336
Summary Don’t use direct call of theme() function Generate HTML as later as possible
Links Render API http://drupal.org/node/933976 http://drupal.org/node/930760 http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_render/7   States  http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_process_states/7 Drupal API http://api.drupal.org   Examples module http://drupal.org/project/examples Cache backends http://drupal.org/project/apc http://drupal.org/project/memcache http://drupal.org/project/filecache
Thank you e-mail:  [email_address] skype: gumanista

More Related Content

PPT
Drupal Javascript for developers
PPT
jQuery and_drupal
PDF
Geodaten & Drupal 7
PPT
Phase2 OpenPublish Presentation SF SemWeb Meetup, April 28, 2009
ZIP
Drupal Development
ZIP
Drupal Development (Part 2)
KEY
Intro To jQuery In Drupal
KEY
JavaScript in Drupal 7: What developers need to know
Drupal Javascript for developers
jQuery and_drupal
Geodaten & Drupal 7
Phase2 OpenPublish Presentation SF SemWeb Meetup, April 28, 2009
Drupal Development
Drupal Development (Part 2)
Intro To jQuery In Drupal
JavaScript in Drupal 7: What developers need to know

What's hot (20)

PPTX
Drupal.js: Best Practices for Managing Javascript in Drupal
PDF
Drupal 7 Theming - Behind the scenes
DOC
How to migrate Cakephp 1.x to 2.x
PDF
Drupal & javascript
PDF
Using RequireJS with CakePHP
ZIP
Learning the basics of the Drupal API
PDF
Drupal Step-by-Step: How We Built Our Training Site, Part 1
PDF
Introducing Assetic: Asset Management for PHP 5.3
PDF
Assetic (Symfony Live Paris)
PPTX
WordPress for developers - phpday 2011
PDF
Drush - use full power - DrupalCamp Donetsk 2014
PDF
Dojo Confessions
PDF
TurboGears2 Pluggable Applications
PPTX
Let's write secure Drupal code! - Drupal Camp Poland 2019
PPTX
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
PDF
Building Large jQuery Applications
PDF
No Coding Necessary: Building User Macros and Dynamic Reports Inside Confluen...
DOC
No Coding Necessary: Building Confluence User Macros Cheat Sheet - Atlassian ...
PDF
Drupal 8: Routing & More
PDF
Introduction To Django (Strange Loop 2011)
Drupal.js: Best Practices for Managing Javascript in Drupal
Drupal 7 Theming - Behind the scenes
How to migrate Cakephp 1.x to 2.x
Drupal & javascript
Using RequireJS with CakePHP
Learning the basics of the Drupal API
Drupal Step-by-Step: How We Built Our Training Site, Part 1
Introducing Assetic: Asset Management for PHP 5.3
Assetic (Symfony Live Paris)
WordPress for developers - phpday 2011
Drush - use full power - DrupalCamp Donetsk 2014
Dojo Confessions
TurboGears2 Pluggable Applications
Let's write secure Drupal code! - Drupal Camp Poland 2019
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Building Large jQuery Applications
No Coding Necessary: Building User Macros and Dynamic Reports Inside Confluen...
No Coding Necessary: Building Confluence User Macros Cheat Sheet - Atlassian ...
Drupal 8: Routing & More
Introduction To Django (Strange Loop 2011)
Ad

Similar to Render API - Pavel Makhrinsky (20)

PDF
Drupal Render API
PDF
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
PDF
Learning PHP for Drupal Theming, DC Chicago 2009
PDF
Drupal Front End PHP
PPT
The Render API in Drupal 7
ODP
Drupal Theme Development - DrupalCon Chicago 2011
PPTX
Drupal Camp Porto - Developing with Drupal: First Steps
PDF
The History and Future of Drupal Theming.
PDF
D7 theming what's new - London
ODP
Drupal 7 Theming - what's new
PPTX
Theming Drupal: Beyond the Look and Feel
KEY
Theme API
PDF
Design to Theme @ CMSExpo
PPT
Drupal 6 in a nutshell
PDF
Drupal theming - a practical approach (European Drupal Days 2015)
PDF
Grok Drupal (7) Theming
ODP
Drupal
PDF
2007 Fsoss Drupal Under The Hood
PDF
Intro to Theming Drupal, FOSSLC Summer Camp 2010
PDF
The Flexibility of Drupal 8
Drupal Render API
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Learning PHP for Drupal Theming, DC Chicago 2009
Drupal Front End PHP
The Render API in Drupal 7
Drupal Theme Development - DrupalCon Chicago 2011
Drupal Camp Porto - Developing with Drupal: First Steps
The History and Future of Drupal Theming.
D7 theming what's new - London
Drupal 7 Theming - what's new
Theming Drupal: Beyond the Look and Feel
Theme API
Design to Theme @ CMSExpo
Drupal 6 in a nutshell
Drupal theming - a practical approach (European Drupal Days 2015)
Grok Drupal (7) Theming
Drupal
2007 Fsoss Drupal Under The Hood
Intro to Theming Drupal, FOSSLC Summer Camp 2010
The Flexibility of Drupal 8
Ad

More from DrupalCampDN (20)

PDF
Drupal - Changing the Web by Connecting Open Minds - Josef Dabernig
ODP
Dependency Injection in Drupal 8 - Стадник АндрейQweqwe
PDF
Our AWS Cloud Journey - Andrew Boag
PDF
Guzzle in Drupal 8 and as a REST client - Артем Мирошник
PDF
Blocks & Layouts in D7 - Josef Dabernig
PPTX
CKEditor в Drupal: тонкая настройка и кастомизация - Osman Seferov
PDF
Drush - use full power - Alexander Schedrov
PPTX
Это Drupal, %username%! - Андрей Черноус
PDF
Migrate - new way site upgrade
PPTX
Caching on highload Drupal site - Alexander Shumenko
PPTX
Rich Text in Drupal - Вадим Валуев
PDF
May the parallelity be with you! Distributed computing using Erlang language ...
PDF
Panels как философия - Alexander Danilenko
PDF
DrupalGap. How to create native application for mobile devices based on Drupa...
PPTX
Twig internals - Maksym MoskvychevTwig internals maksym moskvychev
PPT
Презентация модуля YandexMoney - Yury Glushkov
PDF
Drupal and Outer space - Martin Mayer
PPT
Boost your theming skills - Artem Shymko
PDF
Continious integration - Иван Лещёв
PPT
Rules - Yaroslav Doroshuk
Drupal - Changing the Web by Connecting Open Minds - Josef Dabernig
Dependency Injection in Drupal 8 - Стадник АндрейQweqwe
Our AWS Cloud Journey - Andrew Boag
Guzzle in Drupal 8 and as a REST client - Артем Мирошник
Blocks & Layouts in D7 - Josef Dabernig
CKEditor в Drupal: тонкая настройка и кастомизация - Osman Seferov
Drush - use full power - Alexander Schedrov
Это Drupal, %username%! - Андрей Черноус
Migrate - new way site upgrade
Caching on highload Drupal site - Alexander Shumenko
Rich Text in Drupal - Вадим Валуев
May the parallelity be with you! Distributed computing using Erlang language ...
Panels как философия - Alexander Danilenko
DrupalGap. How to create native application for mobile devices based on Drupa...
Twig internals - Maksym MoskvychevTwig internals maksym moskvychev
Презентация модуля YandexMoney - Yury Glushkov
Drupal and Outer space - Martin Mayer
Boost your theming skills - Artem Shymko
Continious integration - Иван Лещёв
Rules - Yaroslav Doroshuk

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Electronic commerce courselecture one. Pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation theory and applications.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Mobile App Security Testing_ A Comprehensive Guide.pdf
Cloud computing and distributed systems.
Teaching material agriculture food technology
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Electronic commerce courselecture one. Pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation theory and applications.pdf
Machine learning based COVID-19 study performance prediction
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Advanced methodologies resolving dimensionality complications for autism neur...

Render API - Pavel Makhrinsky

  • 1. Render API Pavel Makhrinsky http://donetsk.drupal.ua
  • 2. Render API Pavel Makhrinsky e-mail: [email_address] skype: gumanista
  • 3. Overview Form API successor Drupal 6 ‘theme’ replacement System to build structured arrays into content The way to build presentation layer
  • 4. Theming ways Noob way Drupal 6 way Drupal 7 way
  • 5. Noob way Inline Inline Inline Inline Inline 1 <div class=&quot;nav&quot;> 2 <ul class=&quot;topnav&quot;> 3 <li id=&quot;lefttopnav&quot; <?php if ( $levelone == 'home' ) echo &quot;class= \&quot; current \&quot; &quot; ; ?> > <?php echo $menu1 ; ?> </li> 4 <li <?php if ( $levelone == &quot;item-2&quot; ) echo &quot;class= \&quot; current \&quot; &quot; ; ?> > <?php echo $menu2 ; ?> </li> 5 <li <?php if ( $levelone == &quot;item-3&quot; ) echo &quot;class= \&quot; current \&quot; &quot; ; ?> > <?php echo $menu4 ; ?> </li> 6 <li <?php if ( $levelone == &quot;item-4&quot; ) echo &quot;class= \&quot; current \&quot; &quot; ; ?> > <?php echo $menu6 ; ?> </li> 7 <li <?php if ( $levelone == &quot;item-5&quot; ) echo &quot;class= \&quot; current \&quot; &quot; ; ?> > <?php echo $menu5 ; ?> </li> 8 <li <?php if ( $levelone == 'item-6' ) echo &quot;class= \&quot; current \&quot; &quot; ; ?> > <?php echo $menu7 ; ?> </li> 9 </ul> 10 </div>
  • 6. Drupal 6 way Use theme functions Implement hook_theme Move large markup to templates 1 $items = array ( 'item-1' , 'item-2' , 'item-3' , 'item-4' , 'item-5' ); 2 $output = theme( 'item_list' , $items );
  • 7. Drupal 6 way - advantages Common way to render elements Reusable functions Predictable markup Possibility to change generation Output altering
  • 8. Drupal 6 way - disadvantages Slower performance Caching difficulties Different parameters
  • 9. Drupal 7 way Use renderable arrays Alter content you need 1 $items = array ( 'item-1' , 'item-2' , 'item-3' , 'item-4' , 'item-5' ); 2 $output = array ( 3 '#items' => $items , 4 '#theme' => 'item_list' 5 );
  • 10. Drupal 7 way advantages Content alterable in a common way All the renderable elements have preprocess and process functions Transparent caching Resources could be attached to elements
  • 11. Render API details hook_theme Renderable array structure Content altering #type, #theme, #theme_wrappers #states Resources Performance and caching
  • 12. hook_theme() hook_theme variables | render element file path template function preprocess functions
  • 13. Renderable array structure ‘ #’ elements system elements #children #access #printed #sorted
  • 14. Content altering #pre_render #post_render preprocess and process functions
  • 15. #type Loads defaults from hook_element_info() 1 function module_template_element_info() { 2 return array ( 3 'file_template' => array ( 4 '#name' => 'misc' , 5 '#fileinfo' => array ( 6 'filename' => '[module_name].[name].[extension]' , 7 'path' => 'includes' , 8 'extension' => 'inc' 9 ) 10 ) 11 ); 11 } 1 // If the default values for this element have not been loaded yet, populate 2 // them. 3 if ( isset ( $elements [ '#type' ]) && empty ( $elements [ '#defaults_loaded' ])) { 4 $elements += element_info( $elements [ '#type' ]); 5 }
  • 16. #theme Invokes theme() function 1 // Get the children of the element, sorted by weight. 2 $children = element_children( $elements , TRUE ); 3 4 // Initialize this element's #children, unless a #pre_render callback already 5 // preset #children. 6 if (! isset ( $elements [ '#children' ])) { 7 $elements [ '#children' ] = '' ; 8 } 9 // Call the element's #theme function if it is set. Then any children of the 10 // element have to be rendered there. 11 if ( isset ( $elements [ '#theme' ])) { 12 $elements [ '#children' ] = theme( $elements [ '#theme' ], $elements ); 13 } 14 // If #theme was not set and the element has children, render them now. 15 // This is the same process as drupal_render_children() but is inlined 16 // for speed. 17 if ( $elements [ '#children' ] == '' ) { 18 foreach ( $children as $key ) { 19 $elements [ '#children' ] .= drupal_render( $elements [ $key ]); 20 } 21 }
  • 17. #theme_wrappers Wrap #children element with code 1 // Let the theme functions in #theme_wrappers add markup around the rendered 2 // children. 3 if ( isset ( $elements [ '#theme_wrappers' ])) { 4 foreach ( $elements [ '#theme_wrappers' ] as $theme_wrapper ) { 5 $elements [ '#children' ] = theme( $theme_wrapper , $elements ); 6 } 7 }
  • 18. #states Adds JavaScript to change the state of an element based on another element 1 $form [ 'toggle_me' ] = array ( 2 '#type' => 'checkbox' , 3 '#title' => t( 'Tick this box to type' ), 4 ); 5 $form [ 'settings' ] = array ( 6 '#type' => 'textfield' , 7 '#states' => array ( 8 // Only show this field when the 'toggle_me' checkbox is enabled. 9 'visible' => array ( 10 ':input[name=&quot;toggle_me&quot;]' => array ( 'checked' => TRUE ), 11 ), 12 ), 13 );  
  • 19. Resources #attached property Allow attach CSS JavaScript Libraries Not cached
  • 20. Performance and caching Setting cache for renderable arrays Some cache usage tips
  • 21. #cache 'keys ' => an array of keys which will be concatenated to form the cache key. 'bin ' => the name of the cache bin to be used (as in 'cache' or 'cache_page', etc. 'expire ' => a Unix timestamp indicating the expiration time of the cache. 'granularity ' => a bitmask indicating the cache type. This should be DRUPAL_CACHE_PER_PAGE , DRUPAL_CACHE_PER_ROLE , or DRUPAL_CACHE_PER_USER
  • 22. Some cache usage tips Don’t cache small items Items from #attached not stored with rendered items Use cache targeting Cache items will not be expired until cron runs, regardless of the expiration time used Elements 5 100 500 With #cache 3211 3276 3235 Without #cache 747 4257 18336
  • 23. Summary Don’t use direct call of theme() function Generate HTML as later as possible
  • 24. Links Render API http://drupal.org/node/933976 http://drupal.org/node/930760 http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_render/7 States http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_process_states/7 Drupal API http://api.drupal.org Examples module http://drupal.org/project/examples Cache backends http://drupal.org/project/apc http://drupal.org/project/memcache http://drupal.org/project/filecache
  • 25. Thank you e-mail: [email_address] skype: gumanista