SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
INsReady Inc.        Upgrade Your Creat'v'ty
           引锐信息科技有限公司            升级你的创造力




Deep into Drupal Theming layer


  王景昇 (Jingsheng Wang)
   CEO @ INsReady
      微博: @王景昇
  Twitter: @skyredwang


                           The above QR-Code was
                           generated by qr.insready.com
INsReady Inc.     Upgrade Your Creat'v'ty
             引锐信息科技有限公司         升级你的创造力




Agenda:
  1. Business logic vs. Presentation logic
  2. Data granularity
  3. Theme engines
  4. Two ways to theme
  5. Render elements
  6. The power of theme()
  7. Theme registry
  8. Theming a View, Field, etc
INsReady Inc.        Upgrade Your Creat'v'ty
                  引锐信息科技有限公司            升级你的创造力



Business logic vs. Presentation logic
Drupal separates its business logic from its presentation
logic to the extreme.


 1. To make the code easier to maintain.
 2. To make it possible to easily swap out one layer's
    implementation without having to re-write the other
    layers.
INsReady Inc.   Upgrade Your Creat'v'ty
引锐信息科技有限公司       升级你的创造力
INsReady Inc.   Upgrade Your Creat'v'ty
                引锐信息科技有限公司       升级你的创造力



Case Study: Beer & Food paring

Live site: http:
//greatbrewers.
com/beer-sommelier
INsReady Inc.        Upgrade Your Creat'v'ty
                  引锐信息科技有限公司            升级你的创造力




How do we do it in a Drupal way?
 One solution:
  1. Use Panels to design the 3-column layout
  2. Put 3 Drupal blocks into the 3 columns
  3. 1st block use jQuery/Ajax to retrieve all food content
     types from backend (explanation later)
  4. 2nd block to retrieve the nodes from sub-food-
     content-type based the selection from block 1.
  5. 3rd block to retrieve more nodes....
  6. The backend is powered by Views with an addon
     module called Views Datasource
INsReady Inc.              Upgrade Your Creat'v'ty
                引锐信息科技有限公司                  升级你的创造力



Data granularity




       Above diagram is from <Drupal 7 module development>
INsReady Inc.        Upgrade Your Creat'v'ty
                 引锐信息科技有限公司            升级你的创造力



Theme engines
Drupal support alternative theme engines. But, we will
only cover the Default Drupal theme engine:
PHPTemplate



Two ways to theme
 1. Theme functions: pass data to a PHP function to wrap it
    in markup
 2. Templates: pass data to a template which is a PHP file
    mixed with markup and PHP print statements
INsReady Inc.        Upgrade Your Creat'v'ty
                 引锐信息科技有限公司            升级你的创造力



Theme functions Rules:
 ● First, the name of the theme function follows the
   pattern:
   theme_[theme hook name]

 ● Second, the theme function will only have a single
   parameter, as follows:
  function theme_THEME_HOOK_NAME($variables) {...}

 ● Third, the theme function should return a string that
   contains the rendered representation of the data.
INsReady Inc.          Upgrade Your Creat'v'ty
                   引锐信息科技有限公司              升级你的创造力



The workflow of preprocess functions:
  1. Code calls theme('hook_name', $variables).
  2. theme() calls preprocess functions for hook_name.
  3. Preprocess functions modify variables.
  4. theme() calls actual implementation for hook_name with
     modified variables.



  All preprocess functions take the form of:

  [module]_preprocess_[theme hook name](&$variables)

This is extremely handy for module developers as it allows all
sorts of integration with other modules.
INsReady Inc.         Upgrade Your Creat'v'ty
                 引锐信息科技有限公司             升级你的创造力




Template files
Templates are files primarily containing HTML but with
some PHP statements mixed in using the template's
variables.

Instead of declaring a theme_hook_name() function, a
module would instead create a hook-name.tpl.php file.

All of the PHP in a template should be limited to printing
out variables.
INsReady Inc.      Upgrade Your Creat'v'ty
               引锐信息科技有限公司          升级你的创造力




Order of preprocess execution
 1. template_preprocess()
 2. template_preprocesss_HOOK()
 3. MODULE_preprocess()
 4. MODULE_preprocess_HOOK()
 5. THEME_preprocess()
 6. THEME_preprocess_HOOK()
 7. template_process()
 8. template_processs_HOOK()
 9. MODULE_process()
10. MODULE_process_HOOK()
11. THEME_process()
12. THEME_process_HOOK()
INsReady Inc.        Upgrade Your Creat'v'ty
                  引锐信息科技有限公司            升级你的创造力




Render elements
A Render element is a complex data structure passed as a
single parameter to theme(), as one of its variables. Render
elements are fundamentally nested arrays that can include:

 1. The data to be rendered
 2. Other render elements which are considered "children"
    of the element
 3. An array of structures such as CSS and JavaScript files,
    that should be attached to the page when it is rendered
 4. A list of theme hooks that can be used to theme the
    data
 5. A list of callback functions to run on the element before
    and after it is themed
INsReady Inc.   Upgrade Your Creat'v'ty
          引锐信息科技有限公司       升级你的创造力




The power of theme()
INsReady Inc.             Upgrade Your Creat'v'ty
                     引锐信息科技有限公司                 升级你的创造力



Theme registry
 1. Whether it's a theme function or a template
 2. The list of preprocess functions to run
 3. The list of process functions to run
 4. The path to the template file (which includes whether the original
    module template file is used or a theme version of it.)
 5. The theme function name (which indicates if it's the original module
    theme function or one overridden by a theme.)
 6. The list of variables that should be passed to theme() when this theme
    hook is called and a default value for each variable
 7. Whether, instead of a list of variables, the theme function expects
    a single render element as its parameter, and what that render
    element should be named


 Clean the theme registry every time you change a
 function or a template file in a theme!
INsReady Inc.     Upgrade Your Creat'v'ty
          引锐信息科技有限公司         升级你的创造力




How to theme a View




               Demo Time!
INsReady Inc.   Upgrade Your Creat'v'ty
                                  引锐信息科技有限公司       升级你的创造力




How to theme a Field
For example, use template override:

   ●   field--body--article.tpl.php
   ●   field--article.tpl.php
   ●   field--body.tpl.php
   ●   field.tpl.php
INsReady Inc.   Upgrade Your Creat'v'ty
   引锐信息科技有限公司       升级你的创造力




Questions & Answers
INsReady Inc.     Upgrade Your Creat'v'ty
         引锐信息科技有限公司         升级你的创造力




Thank You/ 谢谢!

SPECIAL THANKS to 陈刚
& 上海万达信息股份有限公司
for sponsoring this training!

    王景昇 (Jingsheng Wang)
     CEO @ INsReady
        微博: @王景昇
    Twitter: @skyredwang

Más contenido relacionado

Similar a Deep into Drupal Theming Layer

OpenERP Technical Memento
OpenERP Technical MementoOpenERP Technical Memento
OpenERP Technical Memento
Odoo
 
NoCode, Data & AI LLM Inside Bootcamp: Episode 6 - Design Patterns: Retrieval...
NoCode, Data & AI LLM Inside Bootcamp: Episode 6 - Design Patterns: Retrieval...NoCode, Data & AI LLM Inside Bootcamp: Episode 6 - Design Patterns: Retrieval...
NoCode, Data & AI LLM Inside Bootcamp: Episode 6 - Design Patterns: Retrieval...
Anant Corporation
 
Resume latest Update
Resume latest UpdateResume latest Update
Resume latest Update
Vaibhav soni
 
Rifartek Robot Training Course - How to use ClientRobot
Rifartek Robot Training Course - How to use ClientRobotRifartek Robot Training Course - How to use ClientRobot
Rifartek Robot Training Course - How to use ClientRobot
Tsai Tsung-Yi
 

Similar a Deep into Drupal Theming Layer (20)

Symphony Driver Essay
Symphony Driver EssaySymphony Driver Essay
Symphony Driver Essay
 
OpenERP Technical Memento
OpenERP Technical MementoOpenERP Technical Memento
OpenERP Technical Memento
 
NoCode, Data & AI LLM Inside Bootcamp: Episode 6 - Design Patterns: Retrieval...
NoCode, Data & AI LLM Inside Bootcamp: Episode 6 - Design Patterns: Retrieval...NoCode, Data & AI LLM Inside Bootcamp: Episode 6 - Design Patterns: Retrieval...
NoCode, Data & AI LLM Inside Bootcamp: Episode 6 - Design Patterns: Retrieval...
 
Wordpress as a framework
Wordpress as a frameworkWordpress as a framework
Wordpress as a framework
 
Os Minnee
Os MinneeOs Minnee
Os Minnee
 
RavenDB overview
RavenDB overviewRavenDB overview
RavenDB overview
 
Drupal
DrupalDrupal
Drupal
 
Workshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfWorkshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdf
 
Top 8 WCM Trends 2010
Top 8 WCM Trends 2010Top 8 WCM Trends 2010
Top 8 WCM Trends 2010
 
Rajkumar_CS_PSG
Rajkumar_CS_PSGRajkumar_CS_PSG
Rajkumar_CS_PSG
 
ServerTemplate Deep Dive
ServerTemplate Deep DiveServerTemplate Deep Dive
ServerTemplate Deep Dive
 
Get things done with Yii - quickly build webapplications
Get things done with Yii - quickly build webapplicationsGet things done with Yii - quickly build webapplications
Get things done with Yii - quickly build webapplications
 
Manage Deployments with Install Profiles and Git
Manage Deployments with Install Profiles and GitManage Deployments with Install Profiles and Git
Manage Deployments with Install Profiles and Git
 
WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!
WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!
WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!
 
Breaking the 2 Pizza Paradox with your Platform as an Application
Breaking the 2 Pizza Paradox with your Platform as an ApplicationBreaking the 2 Pizza Paradox with your Platform as an Application
Breaking the 2 Pizza Paradox with your Platform as an Application
 
Architecting Applications Using Apache Wicket Java2 Days 2009
Architecting Applications Using Apache Wicket   Java2 Days 2009Architecting Applications Using Apache Wicket   Java2 Days 2009
Architecting Applications Using Apache Wicket Java2 Days 2009
 
Resume latest Update
Resume latest UpdateResume latest Update
Resume latest Update
 
Rifartek Robot Training Course - How to use ClientRobot
Rifartek Robot Training Course - How to use ClientRobotRifartek Robot Training Course - How to use ClientRobot
Rifartek Robot Training Course - How to use ClientRobot
 
WordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeWordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcode
 
Acquia Drupal Certification
Acquia Drupal CertificationAcquia Drupal Certification
Acquia Drupal Certification
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Deep into Drupal Theming Layer

  • 1. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Deep into Drupal Theming layer 王景昇 (Jingsheng Wang) CEO @ INsReady 微博: @王景昇 Twitter: @skyredwang The above QR-Code was generated by qr.insready.com
  • 2. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Agenda: 1. Business logic vs. Presentation logic 2. Data granularity 3. Theme engines 4. Two ways to theme 5. Render elements 6. The power of theme() 7. Theme registry 8. Theming a View, Field, etc
  • 3. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Business logic vs. Presentation logic Drupal separates its business logic from its presentation logic to the extreme. 1. To make the code easier to maintain. 2. To make it possible to easily swap out one layer's implementation without having to re-write the other layers.
  • 4. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力
  • 5. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Case Study: Beer & Food paring Live site: http: //greatbrewers. com/beer-sommelier
  • 6. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 How do we do it in a Drupal way? One solution: 1. Use Panels to design the 3-column layout 2. Put 3 Drupal blocks into the 3 columns 3. 1st block use jQuery/Ajax to retrieve all food content types from backend (explanation later) 4. 2nd block to retrieve the nodes from sub-food- content-type based the selection from block 1. 5. 3rd block to retrieve more nodes.... 6. The backend is powered by Views with an addon module called Views Datasource
  • 7. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Data granularity Above diagram is from <Drupal 7 module development>
  • 8. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Theme engines Drupal support alternative theme engines. But, we will only cover the Default Drupal theme engine: PHPTemplate Two ways to theme 1. Theme functions: pass data to a PHP function to wrap it in markup 2. Templates: pass data to a template which is a PHP file mixed with markup and PHP print statements
  • 9. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Theme functions Rules: ● First, the name of the theme function follows the pattern: theme_[theme hook name] ● Second, the theme function will only have a single parameter, as follows: function theme_THEME_HOOK_NAME($variables) {...} ● Third, the theme function should return a string that contains the rendered representation of the data.
  • 10. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 The workflow of preprocess functions: 1. Code calls theme('hook_name', $variables). 2. theme() calls preprocess functions for hook_name. 3. Preprocess functions modify variables. 4. theme() calls actual implementation for hook_name with modified variables. All preprocess functions take the form of: [module]_preprocess_[theme hook name](&$variables) This is extremely handy for module developers as it allows all sorts of integration with other modules.
  • 11. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Template files Templates are files primarily containing HTML but with some PHP statements mixed in using the template's variables. Instead of declaring a theme_hook_name() function, a module would instead create a hook-name.tpl.php file. All of the PHP in a template should be limited to printing out variables.
  • 12. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Order of preprocess execution 1. template_preprocess() 2. template_preprocesss_HOOK() 3. MODULE_preprocess() 4. MODULE_preprocess_HOOK() 5. THEME_preprocess() 6. THEME_preprocess_HOOK() 7. template_process() 8. template_processs_HOOK() 9. MODULE_process() 10. MODULE_process_HOOK() 11. THEME_process() 12. THEME_process_HOOK()
  • 13. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Render elements A Render element is a complex data structure passed as a single parameter to theme(), as one of its variables. Render elements are fundamentally nested arrays that can include: 1. The data to be rendered 2. Other render elements which are considered "children" of the element 3. An array of structures such as CSS and JavaScript files, that should be attached to the page when it is rendered 4. A list of theme hooks that can be used to theme the data 5. A list of callback functions to run on the element before and after it is themed
  • 14. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 The power of theme()
  • 15. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Theme registry 1. Whether it's a theme function or a template 2. The list of preprocess functions to run 3. The list of process functions to run 4. The path to the template file (which includes whether the original module template file is used or a theme version of it.) 5. The theme function name (which indicates if it's the original module theme function or one overridden by a theme.) 6. The list of variables that should be passed to theme() when this theme hook is called and a default value for each variable 7. Whether, instead of a list of variables, the theme function expects a single render element as its parameter, and what that render element should be named Clean the theme registry every time you change a function or a template file in a theme!
  • 16. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 How to theme a View Demo Time!
  • 17. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 How to theme a Field For example, use template override: ● field--body--article.tpl.php ● field--article.tpl.php ● field--body.tpl.php ● field.tpl.php
  • 18. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Questions & Answers
  • 19. INsReady Inc. Upgrade Your Creat'v'ty 引锐信息科技有限公司 升级你的创造力 Thank You/ 谢谢! SPECIAL THANKS to 陈刚 & 上海万达信息股份有限公司 for sponsoring this training! 王景昇 (Jingsheng Wang) CEO @ INsReady 微博: @王景昇 Twitter: @skyredwang