jinja 0.2.1
jinja: ^0.2.1 copied to clipboard
Jinja2 server-side template engine for Dart. Variables, expressions, control structures and template inheritance.
Jinja for Dart #
Jinja server-side template engine port for Dart 2. Variables, expressions, control structures and template inheritance.
Breaking changes #
Before object.field
or object.method()
expressions uses dart:mirrors
methods.
import 'package:jinja/jinja.dart';
// ...
final env = Environment( /* ... */ );
final template = env.fromString('{{ users[0].name }}');
// ...
outStringSink.write(template.render(users: listOfUsers));
// outStringSink.write(template.renderMap({'users': listOfUsers}));
Now to access the fields and methods of the object, (except namespase
, loop
, self
fields and methods), you need to import the get_field
method from the package:jinja/get_field.dart
file and pass it to the Environment
constructor.
Or pass another method.
import 'package:jinja/jinja.dart';
import 'package:jinja/get_field.dart' show getField;
// ...
final Environment = Environment(getField: getField, /* ... */ );
final template = env.fromString('{{ users[0].name }}');
// ...
outStringSink.write(template.render(users: listOfUsers));
// outStringSink.write(template.renderMap({'users': listOfUsers}));
Done #
- Loaders
- FileSystemLoader
- MapLoader (DictLoader)
- Comments
- Variables
- Expressions: variables, literals, subscription, math, comparison, logic, tests, filters, calls
- Filters (not all, see here)
- Tests
- Statements
- Filter
- For (without recursive)
- If
- Set
- Raw
- Inlcude
- Extends
- Block
Example #
Add package to your pubspec.yaml
as a dependency
dependencies:
jinja: ^0.2.0
Import library and use it:
import 'package:jinja/jinja.dart';
// code ...
final Environment = Environment(blockStart: '...');
final template = env.fromString('...source...');
outStringSink.write(template.render(key: value));
// outStringSink.write(template.renderMap({'key': value}));
Note #
Why is this hack used?
In the final version for the production version, templates will be generated and the render function will have named parameters that are used in the template code.
Second option, create new packages:
- jinja_core - core, filters, tests, utils
- jinja_config - yaml based environment config
- jinja_generator - generate pure dart code
- jinja_nodes - or use nodes
Docs #
In progress ...
Contributing #
If you found a bug, just create a new issue or even better fork and issue a pull request with your fix.