Viewing version 2.0.5. Open the latest package page.
mustache_template
2.0.5A templating library that implements the Mustache template specification
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_template/mustache_template.dart';
main() {
var source = '''
{{# names }}
<div>{{ lastname }}, {{ firstname }}</div>
{{/ names }}
{{^ names }}
<div>No names.</div>
{{/ names }}
{{! I am a comment. }}
''';
var template = 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.
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 = Template('{{ author.name }}');
var output = template.renderString({'author': {'name': 'Greg Lowe'}});
Partials - example usage
var partial = Template('{{ foo }}', name: 'partial');
var resolver = (String name) {
if (name == 'partial-name') { // Name of partial tag.
return partial;
}
};
var t = Template('{{> partial-name }}', partialResolver: resolver);
var output = t.renderString({'foo': 'bar'}); // bar
Lambdas - example usage
var t = Template('{{# foo }}');
var lambda = (_) => 'bar';
t.renderString({'foo': lambda}); // bar
var t = Template('{{# foo }}hidden{{/ foo }}');
var lambda = (_) => 'shown';
t.renderString('foo': lambda); // shown
var t = Template('{{# foo }}oi{{/ foo }}');
var lambda = (LambdaContext ctx) => '<b>${ctx.renderString().toUpperCase()}</b>';
t.renderString({'foo': lambda}); // <b>OI</b>
var t = Template('{{# foo }}{{bar}}{{/ foo }}');
var lambda = (LambdaContext ctx) => '<b>${ctx.renderString().toUpperCase()}</b>';
t.renderString({'foo': lambda, 'bar': 'pub'}); // <b>PUB</b>
var t = 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 = Template('{{# foo }}{{bar}}{{/ foo }}');
var lambda = (LambdaContext ctx) => ctx.renderSource(ctx.source + ' {{cmd}}');
t.renderString({'foo': lambda, 'bar': 'pub', 'cmd': 'build'}); // pub build
Changelog
2.0.5
- Updates metadata for move to https://github.com/flutter/core-packages.
- Updates minimum supported SDK version to Flutter 3.38/Dart 3.10.
2.0.4
- Fixes a broken README link to the Mustache manual.
2.0.3
- Updates minimum supported SDK version to Flutter 3.35/Dart 3.9.
- Fixes error handling in LambdaContext by adding missing throw statements.
- Uses StateError instead of Exception for unreachable code in parser.
2.0.2
- Adds emoji support.
2.0.1
- Transfers the package source from https://github.com/jonahwilliams/mustache to https://github.com/flutter/packages.
- Updates minimum supported SDK version to Dart 3.7.
- Updates code for new analysis options.
2.0.0
- Support for null safe dart added.
1.0.0+1
- Fixed regression where lookups from list did not work. Removed failing tests that depend on reflection.
1.0.0
- Forked from original repo. Support for mirrors removed.
Fork
1.1.1
- Fixed error "boolean expression must not be null". Thanks Nico.
1.1.0
- Better support for class members in sections. Thanks to Janice Collins.
- Set the SDK constraint to Dart 2+.
1.0.2
Set the max SDK constraint to <3.0.0.
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
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add 'mustache_template:2.0.5'pubspec.yaml
name: mustache_template
description: A templating library that implements the Mustache template specification
repository: https://github.com/flutter/core-packages/tree/main/third_party/packages/mustache_template
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+mustache_template%22
version: 2.0.5
environment:
sdk: ^3.10.0
dev_dependencies:
test: ^1.16.5
topics:
- template
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 1.0.0 | On demand | Download | edc3574a…d07a0f |
| 1.0.0+1 | On demand | Download | 6c365241…881511 |
| 2.0.0 | On demand | Download | a46e26f9…df5d1c |
| 2.0.1 | On demand | Download | d9aa84d1…3964f6 |
| 2.0.2 | On demand | Download | daa42be7…dfb2de |
| 2.0.3 | On demand | Download | 4326d000…adf307 |
| 2.0.4 | On demand | Download | 544ff0b8…ddb6fc |
| 2.0.5 | Cached · Upstream SHA-256 matched | Download | c689b4d5…996143 |
| 2.0.6 | On demand | Download | 17a8de90…13f3ce |
Dependencies
No direct dependencies declared.