jinja
Jinja (3.x) server-side template engine port for Dart 2. Variables, expressions, control structures and template inheritance.
Version 0.6.0 introduces breaking changes
FilterArgumentErrorerror class removed*argsand**kwargsarguments support removed- Auto-escaping and related statements, filters and tests have been removed due to the impossibility of extending
String. Use theescapefilter manually or escape values before passing them to the template.
For more information, see CHANGELOG.md.
Documentation
It is mostly similar to Jinja templates documentation, differences provided below.
work in progress.
Differences with Python version
- The
defaultfilter compares values withnull. - The
definedandundefinedtests compare values withnull. - The
mapfilter also compares values withnull. Useattributeanditemfilters forobject.attributeandobject[item]expressions. - If
Environment({getAttribute})is not passed, thegetItemmethod will be used. This allows you to use{{ map.key }}as an expression equivalent to{{ map['key'] }}. - String slices and negative indexes are not supported.
- Macro arguments without default values are required.
- Not supported:
- Template module.
- work in progress
Dynamically invoked members (can increase the size of the JS output)
[],+,-,*,/,~/,%operatorsobject.lengthgetterobject.callgetterFunction.apply(function, ...)
Example
import 'package:jinja/jinja.dart';
// ...
var environment = Environment(blockStart: '...', blockEnd: '...');
var template = environment.fromString('...source...');
print(template.render({'key': value}));
// or write directly to StringSink (IOSink, HttpResponse, ...)
template.renderTo(stringSink, {'key': value});
See also examples with conduit and reflectable.
Status:
TODO:
- Informative error messages
- Template name 🔥
- Source span 🔥
Templateclass:generatemethodstreammethod
- Relative template paths
- Async Support
- Template Inheritance
- Super Blocks
{{ super.super() }}
- Super Blocks
- List of Control Structures
- Extends
- Execute non-
blockstatements and expressions{% extends 'base.html' %} {% set title = 'Index' %} {% macro header() %} <h1>{{ title }}</h1> {% endmacro %} ``` {% block body %} {{ header() }} {% endblock %}
- Execute non-
- Extends
- Expressions
- Dart Methods and Properties
!./?.
- Dart Methods and Properties
- Loaders
- PackageLoader (VM)
- ...
- List of Global Functions
lipsumcyclerjoiner
- Extensions
- i18n
- Loop Controls
- Debug Statement
- Template compiler (builder)
- ...
Done:
Note: item - not supported
- Variables
- Filters
forceescapesafeunsafe
- Tests
escaped
- Comments
- Whitespace Control
- Escaping (only
escapefilter) - Line Statements
- Comments
- Blocks
- Template Inheritance
- Base Template
- Child Template
- Super Blocks
- Nesting extends
- Named Block End-Tags
- Block Nesting and Scope
- Required Blocks
- Template Objects
HTML Escaping- List of Control Structures
- For
- If
- Macro
- Call
- Filters
- Assignments
- Block Assignments
- Extends
- Blocks
- Include
- Import
- Import Context Behavior
- Expressions with filters and tests
- Literals
"Hello World"42/123_45642.23/42.1e2/123_456.789['list', 'of', 'objects']('tuple', 'of', 'values'){'dict': 'of', 'key': 'and', 'value': 'pairs'}true/falsenull
- Math
+-///%***
- Comparisons
==!=>>=<<=
- Logic
andornot(expr)
- Other Operators
inis|~()./[]
- If Expression
{{ list.last if list }}{{ user.name if user else 'Guest' }}
- Dart Methods and Properties (if reflection is on)
{{ string.toUpperCase() }}{{ list.add(item) }}
- Literals
- List of Global Functions
dictprintrangelistnamespace
- Loaders
FileSystemLoaderMapLoader(DictLoader)
- Extensions
- Expression Statement
- With Statement
Autoescape Overrides
Contributing
Contributions are welcome! Please open an issue or pull request on GitHub. Look at the ToDo list and comments in the code for ideas on what to work on. There are no strict rules, but please try to follow the existing code style.
As non-native English speaker and learner, I will be grateful for any corrections in the documentation and code comments.
Support
Post issues and feature requests on the GitHub issue tracker.