mustache 0.2.5+1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 71

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'}
	]});

	print(output);
}

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.

Dart2js

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('{{ author.name }}');
  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

CHANGELOG

0.2.5

  • 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).

0.2.4

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

0.2.3

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

0.2.2

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

0.2.1

  • Added new methods to LambdaContext.

0.2

  • 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

Use this package as a library

1. Depend on it

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


dependencies:
  mustache: "^0.2.5+1"

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.1 Jun 3, 2018 Go to the documentation of mustache 1.0.1 Download mustache 1.0.1 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

All 19 versions...

Analysis

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

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3

Scores

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

Platforms

Detected platforms: web, other

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

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 9 errors 23 hints.

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

    line: 75 col: 74
    The getter 'text' isn't defined for the class 'Node'.

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

    line: 150 col: 36
    The getter 'text' isn't defined for the class 'Node'.

    Similar analysis of the following files failed:

    • lib/src/renderer.dart (error)
    • lib/src/scanner.dart (hint)
  • Package is getting outdated.

    The package was released 93 weeks ago.

  • 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.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • 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.

Dependencies

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