Template Engines

Template Engines

Treinamento no Terra sobre template engines e Nunjucks.

572696200604e59baa59ee90d61f7d02?s=128

Jaydson Gomes

April 01, 2015
Tweet

Transcript

  1. Template Engines Nunjucks & Jinja

  2. “Template engines are tools to separate program- logic and presentation

    into two independent parts. This makes the development of both logic and presentation easier, improves flexibility and eases modification and maintenance.” http://www.simple-is-better.org/template
  3. String interpolation

  4. String interpolation

  5. String interpolation

  6. Template logic • placeholders (for string-substitution) • if-defined-conditionals (to test

    presence/absence of some data) • foreach-data-loops (for multi-valued data/lists) • recursive macros (to walk recursive data-structures)
  7. None
  8. Nunjucks • Rich: Powerful language with block inheritance, auto escaping,

    macros, async control • Fast & Lean: High-performant • Small: 8K gzipped runtime with precompiled templates in the browser • Extensible: Crazy extensible with custom filters and extensions • Everywhere: Available in node and all modern web browsers
  9. None
  10. Variables {{ username }} {{ foo.bar }} {{ foo["bar"] }}

  11. Filters {{ foo | title }} {{ foo | join(",")

    }} {{ foo | replace("foo", "bar") | capitalize }}
  12. Template Inheritance

  13. parent.html

  14. my_template.html

  15. OUTPUT

  16. Tags

  17. if {% if variable %} It is true {% endif

    %}
  18. if {% if hungry %} I am hungry {% elif

    tired %} I am tired {% else %} I am good! {% endif %}
  19. for

  20. for

  21. macros

  22. API

  23. API

  24. Sample http://github.tpn.terra.com/jaydson-gomes/zaz-app-card http://dsv-fe01-mia.dev.terra.com/~jaydson.gomes/zaz- app-card/dist/samples/pt.html