# mustache_template 2.0.5

A templating library that implements the Mustache template specification




## Installing
```
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add 'mustache_template:2.0.5'
```
Published: 2026-06-09T19:08:46.681980Z
Expected archive SHA-256: c689b4d59176f8c7a2860aab765d205916aa5b06cfafff7613bfcc42fa996143

Available in cache; verification: upstream_checksum
## Direct dependencies

## Package README (untrusted upstream documentation)
# Mustache templates

A Dart library to parse and render [mustache templates](https://mustache.github.io/).

See the [mustache manual](https://mustache.github.io/mustache.5.html) for detailed usage information.

This library passes all [mustache specification](https://github.com/mustache/spec/tree/master/specs) tests.

## Example usage
```dart
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

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

## Partials - example usage

```dart

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

```dart
var t = Template('{{# foo }}');
var lambda = (_) => 'bar';
t.renderString({'foo': lambda}); // bar
```

```dart
var t = Template('{{# foo }}hidden{{/ foo }}');
var lambda = (_) => 'shown';
t.renderString('foo': lambda); // shown
```

```dart
var t = Template('{{# foo }}oi{{/ foo }}');
var lambda = (LambdaContext ctx) => '<b>${ctx.renderString().toUpperCase()}</b>';
t.renderString({'foo': lambda}); // <b>OI</b>
```

```dart
var t = Template('{{# foo }}{{bar}}{{/ foo }}');
var lambda = (LambdaContext ctx) => '<b>${ctx.renderString().toUpperCase()}</b>';
t.renderString({'foo': lambda, 'bar': 'pub'}); // <b>PUB</b>
```

```dart
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.

```dart
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







 
