mustache4dart 3.0.0-dev.1.0

Mustache for Dart #

Build Status Coverage Status

A simple implementation of Mustache for the Dart language, which passes happily all the mustache v1.1.2+λ specs. If you want to have a look at how it works, just check the tests. For more info, just read further.

Using it #

In order to use the library, just add it to your pubspec.yaml as a dependency.

For dart v1, you should be using the v2 of this package:

  mustache4dart: ">= 2.0.0 < 3.0.0"

For dart v2, you should be using the v3 of this package:

  mustache4dart: ">= 3.0.0 < 4.0.0"

and then import the package

import 'package:mustache4dart/mustache4dart.dart';

and you are good to go. You can use the render toplevel function to render your template.

For example:

var salutation = render('Hello {{name}}!', {'name': 'Bob'});
print(salutation); //shoud print Hello Bob!

Context objects #

mustache4dart will look at your given object for operators, fields or methods. For example, if you give the template {{firstname}} for rendering, mustache4dart will try the followings

  1. use the [] operator with firstname as the parameter
  2. search for a field named firstname
  3. search for a getter named firstname
  4. search for a method named firstname (see Lambdas support)

in each case the first valid value will be used.

Partials #

mustache4dart support partials but it needs somehow to know how to find a partial. You can do that by providing a function that returns a template given a name:

String partialProvider(String partialName) => "this is the partial with name: ${partialName}";
expect(render('[{{>p}}]', null, partial: partialProvider), '[this is the partial with name: p]'));

Compiling to functions #

If you have a template that you are going to reuse with different contexts, you can compile it to a function using the toplevel function compile:

var salut = compile('Hello {{name}}!');
print(salut({'name': 'Alice'})); //should print Hello Alice!

Lambdas support #

The library passes all the optional lambda specs based on which lambdas must be treated as arity 0 or 1 functions. As dart provides optional named parameters, you can pass to a given lambda function the nestedContext. In that case the current nested context will be given as parameter to the lambda function.

Flutter / Browser support #

In order to achive support on targets where dart:mirrors is not allowed, reflectable is being used. In such a case the user should mark the objects used by mustache4dart with @MustacheContext() and make sure that the reflectable builder is being run accordingly.

reflectable setup

The easiest way to get reflectable to work is by making use of build_runner with a build.yaml that should look like:

  # your package name, in my case is mustache4dart
        # a list of dart files containing the annotated code,
        # in my case that is only some test files
          - test/**_test.dart
          formatted: true

Developing #

The project passes all the Mustache specs. You have to make sure though that you've downloaded them. Just make sure that you have done the steps described below.

git clone git://
git submodule init
git submodule update
pub get

If you are with Linux, you can use what travis does:


Alternatively, if you have Dart Test Runner installed you can just do:

pub global run test_runner

Observatory #

To start the observatory after running test:

dart --pause-isolates-on-exit --enable-vm-service=NNNN ./test/mustache_all.dart

Then coverage can be used in order to collect and format data:

pub global run coverage:collect_coverage --uri=http://... -o /tmp/mustache4dart.coverage.json --resume-isolates
pub global run coverage:format_coverage --packages=app_package/.packages -i /tmp/mustache4dart.coverage.json

Contributing #

If you found a bug, just create a new issue or even better fork and issue a pull request with you fix.

Versioning #

The library will follow a semantic versioning


3.0.0-dev.1.0 (2018-08-25) #

3.0.0-dev.0.0 (2018-08-19) #

  • Drop support for dart v1

2.1.1 (2018-04-14) #

  • Addressed Dart 2 analysis issues #70
  • Run tests against v2 vm of Dart

2.1.0 (2017-06-21) #

  • Mirrors improvements #65

2.0.0 (2017-06-13) #

  • Rework MustacheContext (#64)
  • Use dartanalyzer --strong mode (#61)

As part of the MustacheContext rework, a couple of simplifications have been made. Most notable one is the drop support of mirroring methods starting with get as it does not make any sense with dart. Use a getter instead.

1.1.0 (2017-05-10) #

  • Avoid trapping exceptions by using reflection (#59)

1.0.12 (2017-03-17) #

  • Maintenance release

1.0.11 (2017-03-02) #

  • Fixed issue with default value of boolean arguments
  • Compile method now returns type annotation issue

1.0.10 (2015-03-15) #

  • Allow iterables to get queried for empty or isEmpty attribute issue

1.0.9 (2015-02-13) #

  • throw exception on missing property (helps debugging and tracking down errors) issue
  • introduced assumeNullNonExistingProperty (the difference between a null field and a non-existent field) issue
  • Provide lambdas with the current nested context when they have two parameters (lambdas can now render their contents when inside of a loop) issue

1.0.8 (2015-02-01) #

  • Find property names in superclasses issue

Use this package as a library

1. Depend on it

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

  mustache4dart: ^3.0.0-dev.1.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

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

3. Import it

Now in your Dart code, you can use:

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

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

  • Dart: 2.7.1
  • pana: 0.13.4

Health suggestions

Fix lib/src/tokens.dart. (-16.09 points)

Analysis of lib/src/tokens.dart reported 35 hints, including:

line 3 col 1: Prefer using /// for doc comments.

line 21 col 14: Unnecessary new keyword.

line 24 col 14: Unnecessary new keyword.

line 31 col 14: Unnecessary new keyword.

line 33 col 14: Unnecessary new keyword.

Fix lib/src/tmpl.dart. (-10.89 points)

Analysis of lib/src/tmpl.dart reported 23 hints, including:

line 9 col 13: Unnecessary new keyword.

line 11 col 25: Unnecessary new keyword.

line 44 col 12: Unnecessary new keyword.

line 72 col 8: Don't explicitly initialize variables to null.

line 72 col 22: Use = to separate a named parameter from its default value.

Fix lib/src/reflect.dart. (-9.08 points)

Analysis of lib/src/reflect.dart reported 19 hints, including:

line 4 col 21: Avoid const keyword.

line 16 col 20: Avoid const keyword.

line 26 col 39: Use = to separate a named parameter from its default value.

line 28 col 12: Unnecessary new keyword.

line 31 col 12: Unnecessary new keyword.

Fix lib/mustache_context.dart. (-4.41 points)

Analysis of lib/mustache_context.dart reported 9 hints, including:

line 15 col 61: Use = to separate a named parameter from its default value.

line 37 col 12: Unnecessary new keyword.

line 44 col 10: Unnecessary new keyword.

line 49 col 23: Unnecessary new keyword.

line 58 col 8: Don't type annotate initializing formals.

Fix lib/src/mustache.dart. (-2.96 points)

Analysis of lib/src/mustache.dart reported 6 hints, including:

line 6 col 17: Use = to separate a named parameter from its default value.

line 8 col 32: Use = to separate a named parameter from its default value.

line 9 col 39: Use = to separate a named parameter from its default value.

line 18 col 57: Use = to separate a named parameter from its default value.

line 20 col 17: Unnecessary new keyword.

Maintenance suggestions

Package is getting outdated. (-42.74 points)

The package was last published 74 weeks ago.

The package description is too short. (-13 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is pre-release. (-5 points)

Pre-release versions should be used with caution; their API can change in breaking ways.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and mustache4dart.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.64.1 <3.0.0
reflectable ^2.0.4 2.2.1+2
Transitive dependencies
_fe_analyzer_shared 1.0.3
analyzer 0.39.4
args 1.5.2
async 2.4.0
build 1.2.2
build_config 0.4.1+1
build_daemon 2.1.3
build_resolvers 1.3.2
build_runner_core 4.3.0
built_collection 4.3.2
built_value 7.0.8
charcode 1.1.2
checked_yaml 1.0.2
code_builder 3.2.1
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.3.3
fixnum 0.10.11
glob 1.2.0
graphs 0.2.0
html 0.14.0+3
http 0.12.0+4
http_multi_server 2.1.0
http_parser 3.1.3
io 0.3.3
js 0.6.1+1
json_annotation 3.0.1
logging 0.11.4
matcher 0.12.6
meta 1.1.8
mime 0.9.6+3
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0 2.0.0
package_resolver 1.0.10
path 1.6.4
pedantic 1.9.0
pool 1.4.0
pub_semver 1.4.2
pubspec_parse 0.1.5
quiver 2.1.2+1
shelf 0.7.5
shelf_web_socket 0.2.3
source_span 1.6.0
stack_trace 1.9.3
stream_channel 2.0.0
stream_transform 1.1.0
string_scanner 1.0.5
term_glyph 1.1.0
timing 0.1.1+2
typed_data 1.1.6
watcher 0.9.7+13
web_socket_channel 1.1.0
yaml 2.2.0
Dev dependencies
build_runner any 1.7.3
build_test any
isolate ^2.0.0
test ^1.3.0