SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
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

Mais conteúdo relacionado

Semelhante a Deep into Drupal Theming Layer

OpenERP Technical Memento
OpenERP Technical MementoOpenERP Technical Memento
OpenERP Technical MementoOdoo
 
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
 
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.pdfTobiasGoeschel
 
ServerTemplate Deep Dive
ServerTemplate Deep DiveServerTemplate Deep Dive
ServerTemplate Deep DiveRightScale
 
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 webapplicationsGiuliano Iacobelli
 
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 Gitnhepner
 
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!Evan Mullins
 
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 ApplicationMark Rendell
 
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 2009Mystic Coders, LLC
 
Resume latest Update
Resume latest UpdateResume latest Update
Resume latest UpdateVaibhav 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 ClientRobotTsai Tsung-Yi
 
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 shortcodeRakesh Kushwaha
 
Acquia Drupal Certification
Acquia Drupal CertificationAcquia Drupal Certification
Acquia Drupal CertificationPhilip Norton
 

Semelhante 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

SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Último (20)

SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

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