Viewing version 0.2.3. Open the latest package page.
mustache
0.2.3Mustache template library
Readme
Mustache templates
A Dart library to parse and render mustache templates.
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
CHANGELOG
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
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'mustache:0.2.3'pubspec.yaml
name: mustache
version: 0.2.3
author: Greg Lowe <greg@vis.net.nz>
description: Mustache template library
homepage: https://github.com/xxgreg/mustache
environment:
sdk: '>=1.0.0 <2.0.0'
dev_dependencies:
unittest: ">=0.9.0 <0.12.0"
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.1.0 | Cached · Upstream SHA-256 matched | Download | ed4ab6fb…bbb413 |
| 0.1.1 | Cached · Upstream SHA-256 matched | Download | 55219fab…283bb3 |
| 0.1.2 | Cached · Upstream SHA-256 matched | Download | 86679efa…5e7720 |
| 0.1.3 | Cached · Upstream SHA-256 matched | Download | 92a2fcee…825cf8 |
| 0.1.4 | Cached · Upstream SHA-256 matched | Download | 04c94972…0ab0ad |
| 0.1.5 | Cached · Upstream SHA-256 matched | Download | 2d2be5b6…5dd5c0 |
| 0.1.6 | Cached · Upstream SHA-256 matched | Download | ca8409d6…577b2d |
| 0.1.7 | Cached · Upstream SHA-256 matched | Download | e17a32ba…4756be |
| 0.1.8 | Cached · Upstream SHA-256 matched | Download | 0752264e…6ead77 |
| 0.2.0 | Cached · Upstream SHA-256 matched | Download | f80eeec4…9702f3 |
| 0.2.0+1 | Cached · Upstream SHA-256 matched | Download | dd91018f…41528b |
| 0.2.1 | Cached · Upstream SHA-256 matched | Download | ae6f8e4f…9c4f53 |
| 0.2.2 | Cached · Upstream SHA-256 matched | Download | 79b9addd…7fb3a2 |
| 0.2.3 | Cached · Upstream SHA-256 matched | Download | 0f3d176a…01cf1b |
| 0.2.4 | Cached · Upstream SHA-256 matched | Download | fd345452…6f5f46 |
| 0.2.5 | Cached · Upstream SHA-256 matched | Download | a74fb5f2…510297 |
| 0.2.5+1 | Cached · Upstream SHA-256 matched | Download | f3b958a5…fa40fe |
| 1.0.0 | Cached · Upstream SHA-256 matched | Download | ccc823f8…e5d116 |
| 1.0.1 | Cached · Upstream SHA-256 matched | Download | 3e5b7462…811be5 |
| 1.0.2 | Cached · Upstream SHA-256 matched | Download | 959cbf25…4c21c2 |
| 1.1.0 | Cached · Upstream SHA-256 matched | Download | be6cde6e…dcd543 |
| 1.1.1 | Cached · Upstream SHA-256 matched | Download | ee510068…878cd8 |
Dependencies
No direct dependencies declared.