jinja 0.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 68

Jinja for Dart #

Pub

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.

0.2.1 #

  • add pedantic, format

0.2.0 #

  • minimal SDK version: 2.7.0
  • add package:jinja/get_field.dart
  • remove package:jinja/filters.dart
  • remove package:jinja/tests.dart
  • environment:
    • getField and getItem methods
    • leftStripBlock and keepTrailingNewLine fields
  • field and method calling expressions that used mirrors now uses Environment.getField method
    default getField throws runtime error, to use field expression import and pass to environment getField method from package:jinja/get_field.dart, this method uses dart:mirrors
  • filters:
    • add environment filters:
      • batch
      • filesizeformat
  • more package tests

0.1.2 #

  • fix example

0.1.1 #

  • fix template blocks ref

0.1.0 #

  • little speed up
  • new statements:
    • filter
    • set
    • raw
  • add namespace
  • environment
    • add leftStrip and trimBlocks
    • add extensions (can change)
    • rename:
      • stmtOpen to blockStart
      • stmtClose to blockEnd
      • varOpen to variableStart
      • varClose to variableEnd
      • commentOpen to commentStart
      • commentClose to commentEnd
    • move autoReload to FileSystemLoader
    • remove getFilter and getTest
  • update context and parser
  • parser:
    • parsing *args, **kwargs in calls (not in filters and tests)
    • custom tags support
    • context
  • template:
    • update render function: render(kv1: ..., kv2: ...) calls renderMap({'kv1': ..., 'kv2': ...})
    • add self namespace and self.blockName callback ref in context
  • removed filters:
    • batch
    • filesizeformat

0.0.8+1 #

  • Fixes

0.0.8 #

  • Add and update filters
  • Update escaping
  • Update package tests
  • Formatting & fixes

0.0.7+1 #

  • Set (one variable assign statement)
  • Recursive For-Loop
  • Unpacking argument expression
  • Formatting & fixes

0.0.7 #

  • Add global namespace to env and update context
  • Add all missing builtin tests
  • Fix expression group parsing
  • Update package tests
  • Formatting & fixes

0.0.6 #

  • Add undefined & env.finalize
  • Add and switch to env call template, filter, test methods
  • Add is and in expressions
  • Add sum filter
  • Formatting & fixes

0.0.5 #

  • Add batch and escape filters
  • Template and context required env
  • Environment fromSource can store template
  • Formatting & fixes

0.0.4 #

  • Add test for for-loop & if-condition
  • Add defined filter
  • Immutable context
  • Little Docs
  • Formatting & fixes

0.0.3 #

  • Fixes & formatting
  • More filters & tests, package tests

0.0.2 #

  • Fixes & formatting

0.0.1 #

  • Base statements, variables, inheritance, filters & tests and example

example/example.dart

import 'dart:io';

import 'package:jinja/jinja.dart';

void main() {
  final path = Platform.script.resolve('.').toFilePath();

  final env = Environment(
    globals: <String, Object>{
      'now': () {
        final dt = DateTime.now().toLocal();
        final hour = dt.hour < 10 ? '0${dt.hour}' : dt.hour.toString();
        final minute = dt.minute < 10 ? '0${dt.minute}' : dt.minute.toString();
        return '$hour:$minute';
      },
    },
    loader: FileSystemLoader(path: path),
    leftStripBlocks: true,
    trimBlocks: true,
  );

  final template = env.getTemplate('users.html');

  stdout.write(template.render(users: [
    {'fullname': 'Jhon Doe', 'email': 'jhondoe@dev.py'},
    {'fullname': 'Jane Doe', 'email': 'janedoe@dev.py'},
  ]));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  jinja: ^0.2.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:jinja/jinja.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
35
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
68
Learn more about scoring.

We analyzed this package on Jan 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
meta ^1.1.8 1.1.8
path ^1.6.4 1.6.4
string_scanner ^1.0.5 1.0.5
Transitive dependencies
charcode 1.1.2
collection 1.14.12
source_span 1.6.0
term_glyph 1.1.0
Dev dependencies
pedantic ^1.9.0
test ^1.11.0