Skip to content

3.0.0

Latest

Choose a tag to compare

@mattt mattt released this 27 Oct 15:01
a091d6a

Important

This is a major release. Rendering is much closer to Jinja2, so a template that renders today may render differently, and the implementation types are now internal. See the breaking changes below before you upgrade.

Breaking changes

Output

  • true, false, and none print as True, False, and None, and strings inside lists and dicts print quoted, as Python does (#86, #87). Value.description changes the same way, and {{ null }} now prints None.
  • A single newline at the end of a template is removed, as in Jinja2. Set keepTrailingNewline in Template.Options to keep it (#90).
  • Whitespace control ({%-, -}}, and the rest) strips exactly what Jinja2 strips (#93).
  • Filters and tests bind tighter than binary operators and unary minus, so {{ 2 * 3|string }} renders 33 and {{ -1|abs }} renders 1. As in Jinja2, these are now syntax errors: adjacent expressions other than string literals ({{ a b }}), chained tests ({{ x is defined is defined }}), and number forms such as 1e3 and 0x1F, which used to render the wrong value (#96).
  • In {% for x in xs if test %}, the loop variables and the else block follow the filtered items (#92).
  • Iterating an undefined value runs no iterations, and unpacking an item that doesn't fit the loop variables throws (#88).
  • slice distributes and fills items as Jinja2 does (#99).
  • Integer overflow, and integer filter arguments that would hang or trap, throw errors instead of crashing (#91, #94).
  • Macro calls nest at most 16 deep by default, and deeper calls throw an error instead of overflowing the stack. Jinja2 allows more, so a template with 17 or more nested calls no longer renders unless you raise maximumCallDepth on the Environment, which is safe only on a thread with a larger stack (#91, #102).

API

  • render(_:environment:) no longer writes the context into the Environment you pass, so variables don't carry over between renders. Renders can share one Environment across threads, as long as nothing changes it during a render. Context and environment variables now hide built-in globals with the same name, as in Jinja2 (#98).
  • Environment.lstripBlocks and Environment.trimBlocks are removed. They had no effect; use Template.Options (#98).
  • Lexer, Token, Parser, Node, Expression, Statement, Interpreter, PropertyMembers, and Macro's body, defaults, and initializer are now internal (#97). If you used one of these, please open an issue.

Code that uses only Template, Template.Options, render, Value, and Environment compiles without changes, unless it sets lstripBlocks or trimBlocks on an Environment.

Performance

Tokenizing long templates and slicing long strings no longer take quadratic time (#100, #101).

What's Changed

  • Return the default for any falsy input in boolean mode by @mattt in #84
  • Accept separators and sort_keys in tojson by @mattt in #85
  • Print list and dict elements with Python repr by @mattt in #86
  • Print booleans and none as Python's str() does by @mattt in #87
  • Throw errors for unbounded macro recursion and integer overflow by @mattt in #91
  • Unify for loop iteration across arrays, dicts, and strings by @mattt in #88
  • Add str.format and str.join methods on strings by @mattt in #89
  • Drop a single trailing newline from templates by default by @mattt in #90
  • Apply whitespace control in the lexer by @mattt in #93
  • Filter for loop items before computing loop variables by @mattt in #92
  • Throw errors for integer arguments that hang or trap by @mattt in #94
  • Distribute and fill slices as Jinja2 does by @mattt in #99
  • Slice strings in linear time by @mattt in #101
  • Count token positions from the previous token instead of the start by @mattt in #100
  • Bind filters and tests tighter than binary operators by @mattt in #96
  • Render templates without changing the environment by @mattt in #98
  • Make the lexer, parser, AST, and interpreter internal by @mattt in #97
  • Add Jinja2 conformance tests and a weekly fuzz job by @mattt in #95
  • Make the maximum macro call depth configurable by @mattt in #102

Full Changelog: 2.5.2...3.0.0