Make the Most of Twig
www.oroinc.com
Andrey Yatsenko
Passionate about Symfony & DX
- Developer advocate
- Software Engineer
- Trainer
- Symfony 3 Certified Developer
Twig
Why Twig in 2019?
Pros
● Written in PHP
● Easy to learn
● Fast
● Secure
● Flexible
Why Twig in 2019?
Used by many Open-Source projects like
Why Twig in 2019?
Pros
● Written in PHP
● Easy to learn
● Fast
● Secure
● Flexible
● Mature & popular
● Clean errors
● Good support
Why Twig in 2019?
Pros Cons
● Written in PHP
● Easy to learn
● Fast
● Secure
● Flexible
● Mature & popular
● Clean errors
● Good support
● Easy to mess up in a complex project
● Allows too much
Why am I talking about Twig?
Page Templates
At OroCommerce Twig is Used for
Email Templates
CMS Pages
Product Listing Page in OroCommerce
Product Listing Page Controller in OroCommerce
Product Listing Page Controller in OroCommerce
Typical OroCommerce Project
Vendor packages An application (src/)
● 2500+ blocks in 1500+ templates
● 250+ macros
● 250+ functions
● 100+ filters
● 300+ blocks
● in 150+ templates
How to Organize Twig Templates in a Big Project?
Complexity
Extensibility
Complexity
Inheritance vs Includes
Macros vs Includes
Twig Functions vs Sub-Requests
Extensibility
Allow Adding Fields to the User Profile from an Extension
Rendering
Rendering
Calling Event Dispatcher from the Template
Calling Event Dispatcher from the Template
Calling Event Dispatcher from the Template
Calling Event Dispatcher from the Template
Placeholders
Placeholders
Placeholders
Placeholders
Placeholders
Placeholders
How Many Placeholders Do We Need for PLP?
www.oroinc.com
Layout Tree
Tree Representation of a Page:
www.oroinc.com
Blocks & Block Types
options:
route_name: homepage
image_url: website_logo.png
id: logo
options:
route_name: ~
image_url: grayscale_logo.png
id: logo_print
www.oroinc.com
Blocks & Block Types
children:
- page_container [container]
- top_stycky_panel [stycky_panel]
- notification [block]
- logo_print [logo]
id: wrapper
www.oroinc.com
Manipulate the Layout Tree
product_view_layout.yml
www.oroinc.com
Each Layout Block is Rendered with a Twig Block
base_layout.html.twig
www.oroinc.com
Each Layout Block is Rendered with a Twig Block
base_layout.html.twig
User Editable Dynamic Content
Twig Allows Too Much
Solution: Twig Sandbox Extension
Twig Sandboxes in OroCommerce
Email Templates
CMS Pages
Twig Sandbox for CMS Pages
Twig Sandbox for CMS Pages
Twig Sandbox for Email Templates
Is it easy to mess up?
Typical OroCommerce Project
Vendor packages An application (src/)
● 2500+ blocks in 1500+ templates
● 250+ macros
● 250+ functions
● 100+ filters
● 300+ blocks
● in 150+ templates
How to change the copyright in the footer?
# 1 Symfony Profiler
# 2 PhpStorm > Twig Integration
# 3 Symfony > PhpStorm Integration
1. Update Symfony configuration
2. On Windows or Linux install https://github.com/aik099/PhpStormProtocol
# 3 Symfony > PhpStorm Integration
#4 PhpStorm 2019.1 Twig Breakpoints
# 5 Dump all Twig Variables Available in a Template
# 6 Dump Local Twig Reference
Dumps:
● Functions
● Filters
● Tests
● Globals
● Namespaces
#7 Twig Inspector
#7 Twig Inspector
#7 Twig Inspector
Find twig templates and blocks used for rendering HTML pages
https://github.com/oroinc/twig-inspector
● Comments with template and block names
● Visual navigation
● Open the template in your favorite IDE
Summary
Pros
1. Easy to learn
2. Fast
3. Secure
4. Flexible
5. Clear errors
6. Mature & Popular
7. Good support
8. Suitable for big & complex projects
Summary
Cons
1. Easy to mess up in a complex project. Easy to navigate.
2. Allows too much. Easy to limit the functionality.
Twig 3 is Released
● A ton of small tweaks
● Better error messages
● Better performance
● Better consistency
● Cleaner code
To upgrade just update the namespaces
Thank you!

Make the most of twig