Repository navigation
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, andnoneprint asTrue,False, andNone, and strings inside lists and dicts print quoted, as Python does (#86, #87).Value.descriptionchanges the same way, and{{ null }}now printsNone.- A single newline at the end of a template is removed, as in Jinja2. Set
keepTrailingNewlineinTemplate.Optionsto 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 }}renders33and{{ -1|abs }}renders1. 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 as1e3and0x1F, which used to render the wrong value (#96). - In
{% for x in xs if test %}, theloopvariables and theelseblock 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).
slicedistributes 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
maximumCallDepthon theEnvironment, which is safe only on a thread with a larger stack (#91, #102).
API
render(_:environment:)no longer writes the context into theEnvironmentyou pass, so variables don't carry over between renders. Renders can share oneEnvironmentacross 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.lstripBlocksandEnvironment.trimBlocksare removed. They had no effect; useTemplate.Options(#98).Lexer,Token,Parser,Node,Expression,Statement,Interpreter,PropertyMembers, andMacro'sbody,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