reflected_mustache 1.0.4

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 79

Mustache templates - Reflected-Version #

This is a clone of Greg Lowes mustace-package


This 'reflected'-package uses reflection instead of mirrors wich makes it Dart 2.x/Browser compatible

Example #

Live on http://reflected_mustache.example.mikemitterer.at/
Source on GitHub

Mustache / Dart #

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 - browser #

import 'dart:html' as dom;
import 'package:reflected_mustache/mustache.dart';

import 'main.reflectable.dart';

@mustache
class Version {
    final int major;
    final int minor;

    Version(this.major, this.minor);
}

@mustache
class DartLang {
    final String name;
    final Version version;
    final String message;

    DartLang(this.name, this.version, this.message);
}

void main() {
    initializeReflectable();

    final Template template = new Template(
        """
            <div>
            Language: {{name}}<br>
            Version: {{version.major}}.{{version.minor}}<br>
            Comment: {{message}}
            </div>
        """.trim(), lenient: false,htmlEscapeValues: false);
    final DartLang language = new DartLang("Dart",new Version(1,13),"Your Dart app is running.");

    final String content = template.renderString(language);
    final dom.Element child = new dom.Element.html(content);

  dom.querySelector('#content').append(child);

  dom.querySelector("body").classes
      .removeWhere((final String selector) => selector == "loading");
}

Example usage - cmdline #

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.

Reflection #

This library uses reflection to get runtime type information. This means - no mirrors necessary

For more info check out the reflectable-package

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

Change Log for reflected_mustache #

Mustache template library

Unreleased #

Feature #

  • Works under Dart II and with reflectable 2.0.x 03b67f5

Docs #

Refactor #

v1.0 - 2018-07-11

Feature #

  • Mustache supports Key/Value pairs 7e8b1dd

Test #

v0.2 - 2018-07-05

Feature #

  • Dart 2.x ready, works with reflectable ab012d3

Bugs #

This CHANGELOG.md was generated with Changelog for Dart

Use this package as a library

1. Depend on it

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


dependencies:
  reflected_mustache: ^1.0.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:reflected_mustache/mustache.dart';
  
Version Uploaded Documentation Archive
1.0.11 Nov 22, 2018 Go to the documentation of reflected_mustache 1.0.11 Download reflected_mustache 1.0.11 archive
1.0.4 Aug 28, 2018 Go to the documentation of reflected_mustache 1.0.4 Download reflected_mustache 1.0.4 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
76
Health:
Code health derived from static analysis. [more]
84
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

We analyzed this package on May 23, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.1
  • pana: 0.12.16

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in libraries.

Health suggestions

Fix lib/src/scanner.dart. (-6.31 points)

Analysis of lib/src/scanner.dart reported 13 hints, including:

line 8 col 20: Use = to separate a named parameter from its default value.

line 38 col 14: The value of the field '_lenient' isn't used.

line 357 col 11: The top level variable '_QUOTE' isn't used.

line 358 col 11: The top level variable '_APOS' isn't used.

line 363 col 11: The top level variable '_LT' isn't used.

Fix lib/src/renderer.dart. (-2.96 points)

Analysis of lib/src/renderer.dart reported 6 hints, including:

line 98 col 49: Use = to separate a named parameter from its default value.

line 140 col 13: DO use curly braces for all flow control structures.

line 142 col 13: DO use curly braces for all flow control structures.

line 167 col 27: DO use curly braces for all flow control structures.

line 310 col 13: Don't explicitly initialize variables to null.

Fix lib/src/parser.dart. (-2.48 points)

Analysis of lib/src/parser.dart reported 5 hints:

line 39 col 15: Use = to separate a named parameter from its default value.

line 111 col 9: Don't explicitly initialize variables to null.

line 128 col 39: Use = to separate a named parameter from its default value.

line 340 col 57: Use = to separate a named parameter from its default value.

line 344 col 10: Don't explicitly initialize variables to null.

Fix additional 4 files with analysis or formatting issues. (-4.98 points)

Additional issues in the following files:

  • lib/src/lambda_context.dart (4 hints)
  • lib/src/template.dart (3 hints)
  • lib/src/node.dart (2 hints)
  • lib/mustache.dart (1 hint)

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and reflected_mustache.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
logging >=0.11.3 <1.0.0 0.11.3+2
reflectable ^2.0.0 2.0.12
Transitive dependencies
analyzer 0.34.3 0.36.3
args 1.5.1
async 2.2.0
build 1.1.0 1.1.4
build_config 0.3.2 0.4.0
build_daemon 0.2.3 0.6.1
build_resolvers 0.2.3 1.0.5
build_runner_core 2.0.1 3.0.5
built_collection 4.2.2
built_value 6.5.0
charcode 1.1.2
code_builder 3.2.0
collection 1.14.11
convert 2.1.1
crypto 2.0.6
csslib 0.16.0
dart_style 1.2.3 1.2.7
fixnum 0.10.9
front_end 0.1.9+1 0.1.18
glob 1.1.7
graphs 0.2.0
html 0.14.0+2
http 0.12.0+2
http_multi_server 2.0.6
http_parser 3.1.3
io 0.3.3
js 0.6.1+1
json_annotation 2.4.0
kernel 0.3.9+1 0.3.18
matcher 0.12.5
meta 1.1.7
mime 0.9.6+2
package_config 1.0.5
package_resolver 1.0.10
path 1.6.2
pedantic 1.7.0
plugin 0.2.0+3
pool 1.4.0
pub_semver 1.4.2
pubspec_parse 0.1.4
quiver 2.0.3
shelf 0.7.5
shelf_web_socket 0.2.3
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
stream_transform 0.0.19
string_scanner 1.0.4
term_glyph 1.1.0
timing 0.1.1+1
typed_data 1.1.6
watcher 0.9.7+10
web_socket_channel 1.0.13
yaml 2.1.15
Dev dependencies
build_runner any 1.2.3 1.4.0
build_test any
build_web_compilers any
test any