SlideShare uma empresa Scribd logo
1 de 32
Baixar para ler offline
Twig in the Wild
Jeremy Koulish
August 24, 2018
Twig in the Wild
Jeremy Koulish
August 24, 2018
How to Work Drupal Templating Magic
Without Being a PHP Wizard
Key Links
Presentation demo code:
https://github.com/jkoul/twiginthewild
Presentation demo site:
http://jpresentation.dev.bixal.net/
About Me
• BA, Political Economy (’04), Master of Public Policy (’11).
• 10 years in public policy field, in a wide variety of roles.
• Completed General Assembly’s full-stack Web
Development Immersive course in 2015. Focused on
Ruby, JavaScript and basic HTML/CSS.
• Joined Bixal in October 2015 as a front-end dev; a large
part of my role has been theming Drupal 7 and 8 sites.
Why Twig?
Before Drupal 8, Drupal templates were PHP
Which looks like PHP.
node.tpl.php from D7 Bartik theme
In Drupal 8, markup looks more like HTML
With some PHP-ish logic layered on top.
How to Twig
Using Twig
1. Create a custom theme piggybacking off a base theme
(usually “Classy” theme from core).
2. Trigger custom overrides by using specific filenames
within your custom theme’s “templates” folder.
• Those filenames are automagically determined and
prioritized by Twig.
Life-saving Twig helper modules
Twig Debug
helps properly name and scope Twig overrides
sites/development.services.yml Local site’s browser devtools
Life-saving Twig helper modules
Twig Tweak
Loads of useful helpers, including content structure printing.
https://www.drupal.org/docs/8/modules/twig-tweak
Any Twig file Your browser
Examples Context: U.S. Web Design System
• Created in 2015 by 18F and the U.S. Digital Service.
• Comprehensive set of design principles with thorough
supporting documentation
• Plug-and-play UI components
• Open-source file assets available for download
• Contrib ‘uswds’ Drupal 8 theme provides easy theme
layer integration with USWDS asset library: 

https://www.drupal.org/project/uswds
https://designsystem.digital.gov/
That’s all nice…..
but what can you DO with Twig?
Three Major Use Categories
1. Facilitate complex styling, layout and/or behavior by
directly manipulating markup structure.
2. Complement and extend Drupal configuration
3. Hardcode static content directly into the code base
when content cannot – or should not – be edited by
future content creators
•
Use Type #1: Facilitate Complex
Theming and Behavior
Facilitate Complex Theming and Behavior
Add a wrapper element
DEFAULT
CUSTOMIZED
Default template from paragraphs module
Default paragraph template - USWDS contrib theme
Facilitate Complex Theming and Behavior
Remove a wrapper element
DEFAULT CUSTOMIZED
Default region template from classy base theme region—sidebar-first.html.twig (demo site)
Facilitate Complex Theming and Behavior
Change a tag name
DEFAULT CUSTOMIZED
Default field template from classy base theme field--paragraph--field-title.html.twig (demo site)
Facilitate Complex Theming and Behavior
Add attributes to an element
DEFAULT CUSTOMIZED
Default USWDS paragraph template paragraph.html.twig (demo site)
Facilitate Complex Theming and Behavior
• Conditionality
• Loops
• Content manipulation
• Filters
• Set new variables
• Access Drupal-supplied variables
Insert PHP-style logic for dynamic templating
Facilitate Complex Theming and Behavior
Add attributes to an element
DEFAULT
CUSTOMIZED
paragraph.html.twig (demo site)
paragraph—basic-text.html.twig (demo site)
Use Type #2: Complement and
Extend Drupal Configuration
Complement/Extend Drupal Configuration
Editing display config through view modes can get very
complicated, very fast.
Complement/Extend Drupal Configuration
“Alert box” paragraph
uses config-
generated values to
specify type of box,
matching USWDS
specs.
paragraph--alert-box.html.twig
Use Type #3: Hardcode Content
Directly Into the Template
Hardcode Content Directly In the Template
• Hard for content creators/editors to parse.
• Not completely WYSIWYG in complex cases.
• Easy to delete content or internal markup, but much
harder to recover it.
• It already deviates from the Drupal Way.
Because sometimes, a WYSIWYG box just won’t cut it.
Hardcode Content Directly In the Template
Example: official US government site banner
Hardcode Content Directly In the Template
government-banner.html.twig in USWDS contrib theme
Hardcode Content Directly In the Template
Only hardcode content if you don’t
expect it to change anytime soon.
Or ever.
Conclusion
There is no one right way to Twig……but it’s a powerful tool.
USE IT RESPONSIBLY!
Resources
Twig Documentation:
https://twig.symfony.com/doc/1.x/
Twig Tweak docs:
https://www.drupal.org/docs/8/modules/twig-tweak
US Web Design Standards:
https://designsystem.digital.gov/
Presentation demo code:
https://github.com/jkoul/twiginthewild
Presentation demo site:
http://jpresentation.dev.bixal.net/
Thank You!
jeremy.koulish@bixal.com
http://kouli.sh
http://www.bixal.com

Mais conteúdo relacionado

Mais procurados

Frontend Crash Coarse 09/28
Frontend Crash Coarse 09/28Frontend Crash Coarse 09/28
Frontend Crash Coarse 09/28
Ivy Rueb
 
HTML5, CSS3 and the Future of the Web
HTML5, CSS3 and the Future of the WebHTML5, CSS3 and the Future of the Web
HTML5, CSS3 and the Future of the Web
Berg Brandt
 

Mais procurados (15)

What is Drupal? An Introduction to Drupal 8
What is Drupal? An Introduction to Drupal 8What is Drupal? An Introduction to Drupal 8
What is Drupal? An Introduction to Drupal 8
 
Drupal 7 vs. Drupal 8: A Contrast of Multilingual Support
Drupal 7 vs. Drupal 8: A Contrast of Multilingual SupportDrupal 7 vs. Drupal 8: A Contrast of Multilingual Support
Drupal 7 vs. Drupal 8: A Contrast of Multilingual Support
 
Atag & drupal 8
Atag & drupal 8Atag & drupal 8
Atag & drupal 8
 
Develop, Debug, Learn? - Dotjs2019
Develop, Debug, Learn? - Dotjs2019Develop, Debug, Learn? - Dotjs2019
Develop, Debug, Learn? - Dotjs2019
 
Web Programming - 1st TA Session
Web Programming - 1st TA SessionWeb Programming - 1st TA Session
Web Programming - 1st TA Session
 
Frontend Crash Coarse 09/28
Frontend Crash Coarse 09/28Frontend Crash Coarse 09/28
Frontend Crash Coarse 09/28
 
Wywiwyg and Drupal
Wywiwyg and DrupalWywiwyg and Drupal
Wywiwyg and Drupal
 
Html5
Html5Html5
Html5
 
Drupal 8: Most common beginner mistakes
Drupal 8: Most common beginner mistakesDrupal 8: Most common beginner mistakes
Drupal 8: Most common beginner mistakes
 
Drupal Global Training Day by Drupal Mumbai 6th Sep - Drupal Administration
Drupal Global Training Day by Drupal Mumbai 6th Sep - Drupal AdministrationDrupal Global Training Day by Drupal Mumbai 6th Sep - Drupal Administration
Drupal Global Training Day by Drupal Mumbai 6th Sep - Drupal Administration
 
Html5
Html5Html5
Html5
 
HTML5, CSS3 and the Future of the Web
HTML5, CSS3 and the Future of the WebHTML5, CSS3 and the Future of the Web
HTML5, CSS3 and the Future of the Web
 
Flexbox
FlexboxFlexbox
Flexbox
 
Top 20 mistakes you will make on your 1st Drupal project
Top 20 mistakes you will make on your 1st Drupal projectTop 20 mistakes you will make on your 1st Drupal project
Top 20 mistakes you will make on your 1st Drupal project
 
Beginner's guide to drupal
Beginner's guide to drupalBeginner's guide to drupal
Beginner's guide to drupal
 

Semelhante a Twig in the Wild

Twig in the wild august 2018 drupal govcon draft
Twig in the wild   august 2018 drupal govcon draftTwig in the wild   august 2018 drupal govcon draft
Twig in the wild august 2018 drupal govcon draft
JeremyKoulish
 
Introduction To Drupal
Introduction To DrupalIntroduction To Drupal
Introduction To Drupal
Lauren Roth
 

Semelhante a Twig in the Wild (20)

Twig in the wild august 2018 drupal govcon draft
Twig in the wild   august 2018 drupal govcon draftTwig in the wild   august 2018 drupal govcon draft
Twig in the wild august 2018 drupal govcon draft
 
Ready. Set. Drupal! An Intro to Drupal 8, Part 2
Ready. Set. Drupal! An Intro to Drupal 8, Part 2Ready. Set. Drupal! An Intro to Drupal 8, Part 2
Ready. Set. Drupal! An Intro to Drupal 8, Part 2
 
[HKDUG] #20160626 - HKOSCon 2015 - Website DIY with Drupal 8
[HKDUG] #20160626 - HKOSCon 2015 - Website DIY with Drupal 8[HKDUG] #20160626 - HKOSCon 2015 - Website DIY with Drupal 8
[HKDUG] #20160626 - HKOSCon 2015 - Website DIY with Drupal 8
 
Absolute Beginners Guide to Drupal
Absolute Beginners Guide to DrupalAbsolute Beginners Guide to Drupal
Absolute Beginners Guide to Drupal
 
Drupal 7 Search Engine Optimisation
Drupal 7 Search Engine OptimisationDrupal 7 Search Engine Optimisation
Drupal 7 Search Engine Optimisation
 
Building a Documentation Portal
Building a Documentation PortalBuilding a Documentation Portal
Building a Documentation Portal
 
Becoming A Drupal Master Builder
Becoming A Drupal Master BuilderBecoming A Drupal Master Builder
Becoming A Drupal Master Builder
 
Drupal 6x Installation
Drupal 6x Installation Drupal 6x Installation
Drupal 6x Installation
 
October 2016 - USG Rock Eagle - Everything You Need to Know to Plan Your Drup...
October 2016 - USG Rock Eagle - Everything You Need to Know to Plan Your Drup...October 2016 - USG Rock Eagle - Everything You Need to Know to Plan Your Drup...
October 2016 - USG Rock Eagle - Everything You Need to Know to Plan Your Drup...
 
Choosing Drupal as your Content Management Framework
Choosing Drupal as your Content Management FrameworkChoosing Drupal as your Content Management Framework
Choosing Drupal as your Content Management Framework
 
How to Optimize Your Drupal Site with Structured Content
How to Optimize Your Drupal Site with Structured ContentHow to Optimize Your Drupal Site with Structured Content
How to Optimize Your Drupal Site with Structured Content
 
Implementing a Symfony Based CMS in a Publishing Company
Implementing a Symfony Based CMS in a Publishing CompanyImplementing a Symfony Based CMS in a Publishing Company
Implementing a Symfony Based CMS in a Publishing Company
 
Drupal 8 introduction to theming
Drupal 8  introduction to themingDrupal 8  introduction to theming
Drupal 8 introduction to theming
 
Building a Custom Theme in Drupal 8
Building a Custom Theme in Drupal 8Building a Custom Theme in Drupal 8
Building a Custom Theme in Drupal 8
 
Introduction To Drupal
Introduction To DrupalIntroduction To Drupal
Introduction To Drupal
 
Web Components
Web ComponentsWeb Components
Web Components
 
Drupal training-by-ruchiwebsolutions
Drupal training-by-ruchiwebsolutionsDrupal training-by-ruchiwebsolutions
Drupal training-by-ruchiwebsolutions
 
Introduction to the Drupal - Web Experience Toolkit
Introduction to the Drupal - Web Experience ToolkitIntroduction to the Drupal - Web Experience Toolkit
Introduction to the Drupal - Web Experience Toolkit
 
Drupal 8 - Build Week Update
Drupal 8 - Build Week UpdateDrupal 8 - Build Week Update
Drupal 8 - Build Week Update
 
Working with Shortcodes in WordPress
Working with Shortcodes in WordPressWorking with Shortcodes in WordPress
Working with Shortcodes in WordPress
 

Último

Último (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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
 

Twig in the Wild

  • 1. Twig in the Wild Jeremy Koulish August 24, 2018
  • 2. Twig in the Wild Jeremy Koulish August 24, 2018 How to Work Drupal Templating Magic Without Being a PHP Wizard
  • 3. Key Links Presentation demo code: https://github.com/jkoul/twiginthewild Presentation demo site: http://jpresentation.dev.bixal.net/
  • 4. About Me • BA, Political Economy (’04), Master of Public Policy (’11). • 10 years in public policy field, in a wide variety of roles. • Completed General Assembly’s full-stack Web Development Immersive course in 2015. Focused on Ruby, JavaScript and basic HTML/CSS. • Joined Bixal in October 2015 as a front-end dev; a large part of my role has been theming Drupal 7 and 8 sites.
  • 6. Before Drupal 8, Drupal templates were PHP Which looks like PHP. node.tpl.php from D7 Bartik theme
  • 7. In Drupal 8, markup looks more like HTML With some PHP-ish logic layered on top.
  • 9. Using Twig 1. Create a custom theme piggybacking off a base theme (usually “Classy” theme from core). 2. Trigger custom overrides by using specific filenames within your custom theme’s “templates” folder. • Those filenames are automagically determined and prioritized by Twig.
  • 10. Life-saving Twig helper modules Twig Debug helps properly name and scope Twig overrides sites/development.services.yml Local site’s browser devtools
  • 11. Life-saving Twig helper modules Twig Tweak Loads of useful helpers, including content structure printing. https://www.drupal.org/docs/8/modules/twig-tweak Any Twig file Your browser
  • 12. Examples Context: U.S. Web Design System • Created in 2015 by 18F and the U.S. Digital Service. • Comprehensive set of design principles with thorough supporting documentation • Plug-and-play UI components • Open-source file assets available for download • Contrib ‘uswds’ Drupal 8 theme provides easy theme layer integration with USWDS asset library: 
 https://www.drupal.org/project/uswds https://designsystem.digital.gov/
  • 13. That’s all nice….. but what can you DO with Twig?
  • 14. Three Major Use Categories 1. Facilitate complex styling, layout and/or behavior by directly manipulating markup structure. 2. Complement and extend Drupal configuration 3. Hardcode static content directly into the code base when content cannot – or should not – be edited by future content creators •
  • 15. Use Type #1: Facilitate Complex Theming and Behavior
  • 16. Facilitate Complex Theming and Behavior Add a wrapper element DEFAULT CUSTOMIZED Default template from paragraphs module Default paragraph template - USWDS contrib theme
  • 17. Facilitate Complex Theming and Behavior Remove a wrapper element DEFAULT CUSTOMIZED Default region template from classy base theme region—sidebar-first.html.twig (demo site)
  • 18. Facilitate Complex Theming and Behavior Change a tag name DEFAULT CUSTOMIZED Default field template from classy base theme field--paragraph--field-title.html.twig (demo site)
  • 19. Facilitate Complex Theming and Behavior Add attributes to an element DEFAULT CUSTOMIZED Default USWDS paragraph template paragraph.html.twig (demo site)
  • 20. Facilitate Complex Theming and Behavior • Conditionality • Loops • Content manipulation • Filters • Set new variables • Access Drupal-supplied variables Insert PHP-style logic for dynamic templating
  • 21. Facilitate Complex Theming and Behavior Add attributes to an element DEFAULT CUSTOMIZED paragraph.html.twig (demo site) paragraph—basic-text.html.twig (demo site)
  • 22. Use Type #2: Complement and Extend Drupal Configuration
  • 23. Complement/Extend Drupal Configuration Editing display config through view modes can get very complicated, very fast.
  • 24. Complement/Extend Drupal Configuration “Alert box” paragraph uses config- generated values to specify type of box, matching USWDS specs. paragraph--alert-box.html.twig
  • 25. Use Type #3: Hardcode Content Directly Into the Template
  • 26. Hardcode Content Directly In the Template • Hard for content creators/editors to parse. • Not completely WYSIWYG in complex cases. • Easy to delete content or internal markup, but much harder to recover it. • It already deviates from the Drupal Way. Because sometimes, a WYSIWYG box just won’t cut it.
  • 27. Hardcode Content Directly In the Template Example: official US government site banner
  • 28. Hardcode Content Directly In the Template government-banner.html.twig in USWDS contrib theme
  • 29. Hardcode Content Directly In the Template Only hardcode content if you don’t expect it to change anytime soon. Or ever.
  • 30. Conclusion There is no one right way to Twig……but it’s a powerful tool. USE IT RESPONSIBLY!
  • 31. Resources Twig Documentation: https://twig.symfony.com/doc/1.x/ Twig Tweak docs: https://www.drupal.org/docs/8/modules/twig-tweak US Web Design Standards: https://designsystem.digital.gov/ Presentation demo code: https://github.com/jkoul/twiginthewild Presentation demo site: http://jpresentation.dev.bixal.net/