Slide 1

Slide 1 text

Make the Most of Twig

Slide 2

Slide 2 text

www.oroinc.com Andrey Yatsenko Passionate about Symfony & DX - Developer advocate - Software Engineer - Trainer - Symfony 3 Certified Developer

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Twig

Slide 5

Slide 5 text

Why Twig in 2019? Pros ● Written in PHP ● Easy to learn ● Fast ● Secure ● Flexible

Slide 6

Slide 6 text

Why Twig in 2019? Used by many Open-Source projects like

Slide 7

Slide 7 text

Why Twig in 2019? Pros ● Written in PHP ● Easy to learn ● Fast ● Secure ● Flexible ● Mature & popular ● Clean errors ● Good support

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Why am I talking about Twig?

Slide 10

Slide 10 text

Page Templates At OroCommerce Twig is Used for Email Templates CMS Pages

Slide 11

Slide 11 text

Product Listing Page in OroCommerce

Slide 12

Slide 12 text

Product Listing Page Controller in OroCommerce

Slide 13

Slide 13 text

Product Listing Page Controller in OroCommerce

Slide 14

Slide 14 text

Typical OroCommerce Project Vendor packages An application (src/) ● 2500+ blocks in 1500+ templates ● 250+ macros ● 250+ functions ● 100+ filters ● 300+ blocks ● in 150+ templates

Slide 15

Slide 15 text

How to Organize Twig Templates in a Big Project? Complexity Extensibility

Slide 16

Slide 16 text

Complexity

Slide 17

Slide 17 text

Inheritance vs Includes

Slide 18

Slide 18 text

Macros vs Includes

Slide 19

Slide 19 text

Twig Functions vs Sub-Requests

Slide 20

Slide 20 text

Extensibility

Slide 21

Slide 21 text

Allow Adding Fields to the User Profile from an Extension

Slide 22

Slide 22 text

Rendering

Slide 23

Slide 23 text

Rendering

Slide 24

Slide 24 text

Calling Event Dispatcher from the Template

Slide 25

Slide 25 text

Calling Event Dispatcher from the Template

Slide 26

Slide 26 text

Calling Event Dispatcher from the Template

Slide 27

Slide 27 text

Calling Event Dispatcher from the Template

Slide 28

Slide 28 text

Placeholders

Slide 29

Slide 29 text

Placeholders

Slide 30

Slide 30 text

Placeholders

Slide 31

Slide 31 text

Placeholders

Slide 32

Slide 32 text

Placeholders

Slide 33

Slide 33 text

Placeholders

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

How Many Placeholders Do We Need for PLP?

Slide 36

Slide 36 text

www.oroinc.com Layout Tree Tree Representation of a Page:

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

www.oroinc.com Blocks & Block Types children: - page_container [container] - top_stycky_panel [stycky_panel] - notification [block] - logo_print [logo] id: wrapper

Slide 39

Slide 39 text

www.oroinc.com Manipulate the Layout Tree product_view_layout.yml

Slide 40

Slide 40 text

www.oroinc.com Each Layout Block is Rendered with a Twig Block base_layout.html.twig

Slide 41

Slide 41 text

www.oroinc.com Each Layout Block is Rendered with a Twig Block base_layout.html.twig

Slide 42

Slide 42 text

User Editable Dynamic Content

Slide 43

Slide 43 text

Twig Allows Too Much

Slide 44

Slide 44 text

Solution: Twig Sandbox Extension

Slide 45

Slide 45 text

Twig Sandboxes in OroCommerce Email Templates CMS Pages

Slide 46

Slide 46 text

Twig Sandbox for CMS Pages

Slide 47

Slide 47 text

Twig Sandbox for CMS Pages

Slide 48

Slide 48 text

Twig Sandbox for Email Templates

Slide 49

Slide 49 text

Is it easy to mess up?

Slide 50

Slide 50 text

Typical OroCommerce Project Vendor packages An application (src/) ● 2500+ blocks in 1500+ templates ● 250+ macros ● 250+ functions ● 100+ filters ● 300+ blocks ● in 150+ templates

Slide 51

Slide 51 text

How to change the copyright in the footer?

Slide 52

Slide 52 text

# 1 Symfony Profiler

Slide 53

Slide 53 text

# 2 PhpStorm > Twig Integration

Slide 54

Slide 54 text

# 3 Symfony > PhpStorm Integration 1. Update Symfony configuration 2. On Windows or Linux install https://github.com/aik099/PhpStormProtocol

Slide 55

Slide 55 text

# 3 Symfony > PhpStorm Integration

Slide 56

Slide 56 text

#4 PhpStorm 2019.1 Twig Breakpoints

Slide 57

Slide 57 text

# 5 Dump all Twig Variables Available in a Template

Slide 58

Slide 58 text

# 6 Dump Local Twig Reference Dumps: ● Functions ● Filters ● Tests ● Globals ● Namespaces

Slide 59

Slide 59 text

#7 Twig Inspector

Slide 60

Slide 60 text

#7 Twig Inspector

Slide 61

Slide 61 text

#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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

Summary Cons 1. Easy to mess up in a complex project. Easy to navigate. 2. Allows too much. Easy to limit the functionality.

Slide 64

Slide 64 text

Twig 3 is Released ● A ton of small tweaks ● Better error messages ● Better performance ● Better consistency ● Cleaner code To upgrade just update the namespaces

Slide 65

Slide 65 text

Thank you!