mustache4dart 1.0.4

Mustache for the dartlang Build Status

A simple implementation of Mustache for the Dart language, which passes happily all the mustache 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.yalm as a dependency

dependencies:
  mustache4dart: '>= 1.0.0'

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
  5. search for a method named getFirstname

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. The implementation does use the @MirrorsUsed annotation but 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 it will be the only check made against them (from the ones described above) in order to define a value.

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 contextes 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!

Running the tests

At the moment the project is under heavy development but pass all the Mustache specs. If you want to run the tests yourself, just do what drone.io does, or to put it by another way, do the following:

git clone git://github.com/valotas/mustache4dart.git
git submodule init
git submodule update 
pub install
test/run.sh

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

1. Depend on it

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

dependencies:
  mustache4dart: ">=1.0.4 <2.0.0"

If your package is an application package you should use any as the version constraint.

2. Install it

If you're using the Dart Editor, choose:

Menu > Tools > Pub Install

Or if you want to install from the command line, run:

$ pub install

3. Import it

Now in your Dart code, you can use:

import 'package:mustache4dart/mustache4dart.dart';

About

A mustache implementation for the Dart language

Author

Email valotas@gmail.com Georgios Valotasios

Homepage

https://github.com/valotas/mustache4dart

Uploader

valotas

Share