Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Make the most of twig

Make the most of twig

Twig is the most powerful templating engine in the PHP world that enables us to create highly complex projects with hundreds of multi-level extended templates, thousands of blocks, functions, filters, tests, and macros. It also offers a sandbox, a unique but not a widely used feature that is creating secure user-editable templates. In addition, there are a number of handy built-in and external debugging tools available in the Twig ecosystem to simplify the day-to-day work process for a Twig designer.

In this presentation, I will talk about how extensively we use Twig in a complex open-source e-commerce project.

Andrey Yatsenco

November 22, 2019
Tweet

More Decks by Andrey Yatsenco

Other Decks in Technology

Transcript

  1. www.oroinc.com Andrey Yatsenko Passionate about Symfony & DX - Developer

    advocate - Software Engineer - Trainer - Symfony 3 Certified Developer
  2. Why Twig in 2019? Pros • Written in PHP •

    Easy to learn • Fast • Secure • Flexible
  3. Why Twig in 2019? Pros • Written in PHP •

    Easy to learn • Fast • Secure • Flexible • Mature & popular • Clean errors • Good support
  4. 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
  5. Typical OroCommerce Project Vendor packages An application (src/) • 2500+

    blocks in 1500+ templates • 250+ macros • 250+ functions • 100+ filters • 300+ blocks • in 150+ templates
  6. 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
  7. www.oroinc.com Blocks & Block Types children: - page_container [container] -

    top_stycky_panel [stycky_panel] - notification [block] - logo_print [logo] id: wrapper
  8. Typical OroCommerce Project Vendor packages An application (src/) • 2500+

    blocks in 1500+ templates • 250+ macros • 250+ functions • 100+ filters • 300+ blocks • in 150+ templates
  9. # 3 Symfony > PhpStorm Integration 1. Update Symfony configuration

    2. On Windows or Linux install https://github.com/aik099/PhpStormProtocol
  10. # 6 Dump Local Twig Reference Dumps: • Functions •

    Filters • Tests • Globals • Namespaces
  11. #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
  12. 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
  13. Summary Cons 1. Easy to mess up in a complex

    project. Easy to navigate. 2. Allows too much. Easy to limit the functionality.
  14. Twig 3 is Released • A ton of small tweaks

    • Better error messages • Better performance • Better consistency • Cleaner code To upgrade just update the namespaces