mustache4dart2 0.1.0

  • Readme
  • Changelog
  • Installing
  • 57

Mustache for Dart #

Build Status Coverage Status

A simple implementation of Mustache for the Dart language, which passes happily all the mustache v1.1.2+λ specs. If you want to have a look at how it works, just check the tests. For more info, just read further.

Using it #

In order to use the library, just add it to your pubspec.yaml as a dependency

dependencies:
  mustache4dart2: '>= 2.0.0 < 3.0.0'

and then import the package

import 'package:mustache4dart2/mustache4dart2.dart';

and you are good to go. You can use the render toplevel function to render your template.

For example:

var salutation = render('Hello {{name}}!', {'name': 'Bob'});
print(salutation); //shoud print Hello Bob!

Context objects #

mustache4dart will look at your given object for operators, fields or methods. For example, if you give the template {{firstname}} for rendering, mustache4dart will try the followings

  1. use the [] operator with firstname as the parameter
  2. search for a field named firstname
  3. search for a getter named firstname
  4. search for a method named firstname (see Lambdas support)

in each case the first valid value will be used.

@MirrorsUsed #

In order to do the stuff described above the mirror library is being used which could lead to big js files when compiling the library with dartjs. In order to preserve the type information you have to annotate the objects used as contextes with @MirrorsUsed. Have in mind though that as documented this is experimental.

In order to avoid the use of the mirrors package, make sure that you compile your library with dart2js -DMIRRORS=false. In that case though you must always make sure that your context object have a right implementation of the [] operator as no other checks on the object will be available.

Partials #

mustache4dart support partials but it needs somehow to know how to find a partial. You can do that by providing a function that returns a template given a name:

String partialProvider(String partialName) => "this is the partial with name: ${partialName}";
expect(render('[{{>p}}]', null, partial: partialProvider), '[this is the partial with name: p]'));

Compiling to functions #

If you have a template that you are going to reuse with different contexts, you can compile it to a function using the toplevel function compile:

var salut = compile('Hello {{name}}!');
print(salut({'name': 'Alice'})); //should print Hello Alice!

Lambdas support #

The library passes all the optional lambda specs based on which lambdas must be treatable as arity 0 or 1 functions. As dart provides optional named parameters, you can pass to a given lambda function the nestedContext. In that case the current nested context will be given as parameter to the lambda function.

Developing #

The project passes all the Mustache specs. You have to make sure though that you've downloaded them. Just make sure that you have done the steps described below.

git clone git://github.com/valotas/mustache4dart.git
git submodule init
git submodule update
pub get

If you are with Linux, you can use what travis does:

./build.sh

Alternatively, if you have Dart Test Runner installed you can just do:

pub global run test_runner

Observatory #

To start the observatory after running test:

dart --pause-isolates-on-exit --enable-vm-service=NNNN ./test/mustache_all.dart

Then coverage can be used in order to collect and format data:

pub global run coverage:collect_coverage --uri=http://... -o /tmp/mustache4dart.coverage.json --resume-isolates
pub global run coverage:format_coverage --packages=app_package/.packages -i /tmp/mustache4dart.coverage.json

Contributing #

If you found a bug, just create a new issue or even better fork and issue a pull request with you fix.

Versioning #

The library will follow a semantic versioning

CHANGELOG #

2.1.1 (2018-04-14) #

  • Addressed Dart 2 analysis issues #70
  • Run tests against v2 vm of Dart

2.1.0 (2017-06-21) #

  • Mirrors improvements #65

2.0.0 (2017-06-13) #

  • Rework MustacheContext (#64)
  • Use dartanalyzer --strong mode (#61)

As part of the MustacheContext rework, a couple of simplifications have been made. Most notable one is the drop support of mirroring methods starting with get as it does not make any sense with dart. Use a getter instead.

1.1.0 (2017-05-10) #

  • Avoid trapping exceptions by using reflection (#59)

1.0.12 (2017-03-17) #

  • Maintenance release

1.0.11 (2017-03-02) #

  • Fixed issue with default value of boolean arguments
  • Compile method now returns type annotation issue

1.0.10 (2015-03-15) #

  • Allow iterables to get queried for empty or isEmpty attribute issue

1.0.9 (2015-02-13) #

  • throw exception on missing property (helps debugging and tracking down errors) issue
  • introduced assumeNullNonExistingProperty (the difference between a null field and a non-existent field) issue
  • Provide lambdas with the current nested context when they have two parameters (lambdas can now render their contents when inside of a loop) issue

1.0.8 (2015-02-01) #

  • Find property names in superclasses issue

Use this package as a library

1. Depend on it

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


dependencies:
  mustache4dart2: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:mustache4dart2/mustache4dart2.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
27
Health:
Code health derived from static analysis. [more]
84
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
93
Overall:
Weighted score of the above. [more]
57
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: web, other

Primary library: package:mustache4dart2/mustache4dart2.dart with components: mirrors.

Health suggestions

Fix lib/src/tokens.dart. (-7.71 points)

Analysis of lib/src/tokens.dart reported 16 hints, including:

line 3 col 1: Prefer using /// for doc comments.

line 39 col 7: DO use curly braces for all flow control structures.

line 49 col 3: Avoid return types on setters.

line 56 col 3: Prefer using /// for doc comments.

line 61 col 3: Avoid return types on setters.

Fix lib/src/tmpl.dart. (-4.41 points)

Analysis of lib/src/tmpl.dart reported 9 hints, including:

line 72 col 8: Don't explicitly initialize variables to null.

line 72 col 22: Use = to separate a named parameter from its default value.

line 73 col 34: Use = to separate a named parameter from its default value.

line 74 col 41: Use = to separate a named parameter from its default value.

line 127 col 12: Use = to separate a named parameter from its default value.

Fix lib/src/mustache.dart. (-1.99 points)

Analysis of lib/src/mustache.dart reported 4 hints:

line 6 col 17: Use = to separate a named parameter from its default value.

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

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

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

Fix lib/src/mirrors.dart. (-1.49 points)

Analysis of lib/src/mirrors.dart reported 3 hints:

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

line 105 col 24: Always true because length is always greater or equal 0.

line 125 col 24: Use = to separate a named parameter from its default value.

Fix lib/mustache_context.dart. (-1 points)

Analysis of lib/mustache_context.dart reported 2 hints:

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

line 58 col 8: Don't type annotate initializing formals.

Maintenance suggestions

The package description is too short. (-7 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 mustache4dart2.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
Dev dependencies
isolate ^2.0.2
test ^1.0.0