OLD | NEW |
(Empty) | |
| 1 # Mustache templates |
| 2 |
| 3 A Dart library to parse and render [mustache templates](http://mustache.github.c
om/mustache.5.html). |
| 4 |
| 5 [](https:
//drone.io/github.com/xxgreg/mustache/latest) |
| 6 |
| 7 ## Example |
| 8 ```dart |
| 9 import 'package:mustache/mustache.dart' as mustache; |
| 10 |
| 11 main() { |
| 12 var source = '{{#names}}<div>{{lastname}}, {{firstname}}</div>{{
/names}}'; |
| 13 var template = mustache.parse(source); |
| 14 var output = template.renderString({'names': [ |
| 15 {'firstname': 'Greg', 'lastname': 'Lowe'}, |
| 16 {'firstname': 'Bob', 'lastname': 'Johnson'} |
| 17 ]}); |
| 18 print(output); |
| 19 } |
| 20 ``` |
| 21 |
| 22 ## API |
| 23 |
| 24 ```dart |
| 25 |
| 26 Template parse(String source, {bool lenient : false}); |
| 27 |
| 28 abstract class Template { |
| 29 String renderString(values, {bool lenient : false, bool htmlEscapeValues
: true}); |
| 30 void render(values, StringSink sink, {bool lenient : false, bool htmlEsc
apeValues : true}); |
| 31 } |
| 32 |
| 33 ``` |
| 34 |
| 35 Once a template has been created it can be rendered any number of times. |
| 36 |
| 37 Both parse and render throw a FormatException if there is a problem with the tem
plate or rendering the values. |
| 38 |
| 39 When lenient mode is enabled tag names may use any characters, otherwise only a-
z, A-Z, 0-9, underscore and minus. Lenient mode will also silently ignore nulls
passed as values. |
| 40 |
| 41 By default all variables are html escaped, this behaviour can be changed by pass
ing htmlEscapeValues : false. |
| 42 |
| 43 |
| 44 ## Supported |
| 45 ``` |
| 46 Variables {{var-name}} |
| 47 Sections {{#section}}Blah{{/section}} |
| 48 Inverse sections {{^section}}Blah{{/section}} |
| 49 Comments {{! Not output. }} |
| 50 Unescaped variables {{{var-name}}} and {{&var-name}} |
| 51 ``` |
| 52 See the [mustache templates tutorial](http://mustache.github.com/mustache.5.html
) for more information. |
| 53 |
| 54 Passing all [mustache specification](https://github.com/mustache/spec/tree/maste
r/specs) tests for interpolation, sections, inverted, comments. The following se
ctions are not implemented: partials, lambdas and delimeters. |
| 55 |
| 56 ## To do |
| 57 ``` |
| 58 Lenient nulls in inverse sections - see commented out test. |
| 59 Partial tags {{>partial}} |
| 60 Allow functions as values (Lambdas) |
| 61 Set Delimiter tags |
| 62 ``` |
| 63 |
OLD | NEW |