mustache 1.0.0

  • Installing
  • Versions
  • 94

Mustache templates

A Dart library to parse and render mustache templates.

Build Status Coverage Status

See the mustache manual for detailed usage information.

This library passes all mustache specification tests.

Example usage

import 'package:mustache/mustache.dart';

main() {
	var source = '''
	  {{# names }}
            <div>{{ lastname }}, {{ firstname }}</div>
	  {{/ names }}
	  {{^ names }}
	    <div>No names.</div>
	  {{/ names }}
	  {{! I am a comment. }}

	var template = new Template(source, name: 'template-filename.html');

	var output = template.renderString({'names': [
		{'firstname': 'Greg', 'lastname': 'Lowe'},
		{'firstname': 'Bob', 'lastname': 'Johnson'}


A template is parsed when it is created, after parsing it can be rendered any number of times with different values. A TemplateException is thrown if there is a problem parsing or rendering the template.

The Template contstructor allows passing a name, this name will be used in error messages. When working with a number of templates, it is important to pass a name so that the error messages specify which template caused the error.

By default all output from {{variable}} tags is html escaped, this behaviour can be changed by passing htmlEscapeValues : false to the Template constructor. You can also use a {{{triple mustache}}} tag, or a unescaped variable tag {{&unescaped}}, the output from these tags is not escaped.


This library uses mirrors. When compiling with dart2js you will need to pass the experimental mirrors flag. You also need to mark any objects which will be rendered with the @mustache annotation. There is also another version of this library available which doesn't use mirrors.

Differences between strict mode and lenient mode.

Strict mode (default)

  • Tag names may only contain the characters a-z, A-Z, 0-9, underscore, period and minus. Other characters in tags will cause a TemplateException to be thrown during parsing.

  • During rendering, if no map key or object member which matches the tag name is found, then a TemplateException will be thrown.

Lenient mode

  • Tag names may use any characters.
  • During rendering, if no map key or object member which matches the tag name is found, then silently ignore and output nothing.

Nested paths

  var t = new Template('{{ }}');
  var output = template.renderString({'author': {'name': 'Greg Lowe'}});

Partials - example usage

var partial = new Template('{{ foo }}', name: 'partial');

var resolver = (String name) {
   if (name == 'partial-name') { // Name of partial tag.
     return partial;

var t = new Template('{{> partial-name }}', partialResolver: resolver);

var output = t.renderString({'foo': 'bar'}); // bar

Lambdas - example usage

var t = new Template('{{# foo }}');
var lambda = (_) => 'bar';
t.renderString({'foo': lambda}); // bar
var t = new Template('{{# foo }}hidden{{/ foo }}');
var lambda = (_) => 'shown'};
t.renderString({'foo': lambda); // shown
var t = new Template('{{# foo }}oi{{/ foo }}');
var lambda = (LambdaContext ctx) => '<b>${ctx.renderString().toUpperCase()}</b>';
t.renderString({'foo': lambda}); // <b>OI</b>
var t = new Template('{{# foo }}{{bar}}{{/ foo }}');
var lambda = (LambdaContext ctx) => '<b>${ctx.renderString().toUpperCase()}</b>';
t.renderString({'foo': lambda, 'bar': 'pub'}); // <b>PUB</b>
var t = new Template('{{# foo }}{{bar}}{{/ foo }}');
var lambda = (LambdaContext ctx) => '<b>${ctx.renderString().toUpperCase()}</b>';
t.renderString({'foo': lambda, 'bar': 'pub'}); // <b>PUB</b>

In the following example LambdaContext.renderSource(source) re-parses the source string in the current context, this is the default behaviour in many mustache implementations. Since re-parsing the content is slow, and often not required, this library makes this step optional.

var t = new Template('{{# foo }}{{bar}}{{/ foo }}');
var lambda = (LambdaContext ctx) => ctx.renderSource(ctx.source + ' {{cmd}}')};
t.renderString({'foo': lambda, 'bar': 'pub', 'cmd': 'build'}); // pub build



  • Remove MustacheFormatException
  • Allow templates to specify default delimiters. Thanks to Joris Hermans.
  • Fix #24: renderString shrinks multiple newlines to just one (Thanks to John Ryan for the repro).


  • Fix #23 failure if tag or comment contains "="


  • Change handling of lenient sections to match python mustache implementation.


  • Fix MirrorsUsed tag for using mirrors on dart2js.
  • Clean up dead code.


  • Added new methods to LambdaContext.


  • Deprecated parse() function - please update your code to use new Template(source).
  • Deprecated MustacheFormatException - please update your code to use TemplateException.
  • Breaking change: Template.render and Template.renderString methods no longer take the optional lenient and htmlEscapeValues. These should now be passed to the Template constructor.
  • Fully passing all mustache spec tests.
  • Added support for MirrorsUsed.
  • Implemented partials. #11
  • Implemented lambdas. #4
  • Implemented change delimiter tag.
  • Add template name parameter, and show this in error messages.
  • Allow whitespace at begining of tags. #10

1. Depend on it

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

  mustache: "^1.0.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:mustache/mustache.dart';
Version Uploaded Documentation Archive
1.0.0 Nov 20, 2017 Go to the documentation of mustache 1.0.0 Download mustache 1.0.0 archive
0.2.5+1 Sep 25, 2016 Go to the documentation of mustache 0.2.5+1 Download mustache 0.2.5+1 archive
0.2.5 Nov 7, 2015 Go to the documentation of mustache 0.2.5 Download mustache 0.2.5 archive
0.2.4 Oct 28, 2015 Go to the documentation of mustache 0.2.4 Download mustache 0.2.4 archive
0.2.3 May 7, 2015 Go to the documentation of mustache 0.2.3 Download mustache 0.2.3 archive
0.2.2 Apr 22, 2015 Go to the documentation of mustache 0.2.2 Download mustache 0.2.2 archive
0.2.1 Mar 6, 2015 Go to the documentation of mustache 0.2.1 Download mustache 0.2.1 archive
0.2.0+1 Feb 12, 2015 Go to the documentation of mustache 0.2.0+1 Download mustache 0.2.0+1 archive
0.2.0 Feb 12, 2015 Go to the documentation of mustache 0.2.0 Download mustache 0.2.0 archive
0.1.8 Jan 29, 2014 Go to the documentation of mustache 0.1.8 Download mustache 0.1.8 archive

All 18 versions...


We analyzed this package on Apr 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.49.0
  • pana: 0.10.6


Describes how popular the package is relative to other packages. [more]
92 / 100
Code health derived from static analysis. [more]
95 / 100
Reflects how tidy and up-to-date the package is. [more]
99 / 100
Overall score:
Weighted score of the above. [more]
Learn more about scoring.


Detected platforms: web, other

Primary library: package:mustache/mustache.dart with components: mirrors.


  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 error 15 hints.

    Strong-mode analysis of lib/src/renderer.dart failed with the following error:

    line: 94 col: 15
    'value' isn't a function.

    Run dartfmt to format lib/src/lambda_context.dart.

    Similar analysis of the following files failed:

    • lib/src/parser.dart (hint)
    • lib/src/scanner.dart (hint)
  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use mustache.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.0.0 <2.0.0
Dev dependencies
unittest >=0.9.0 <0.13.0