commit | 144a4166c150fd9fd1e7ac8ee8d89600e5f06e53 | [log] [tgz] |
---|---|---|
author | Georgios Valotasios <valotas@gmail.com> | Sat Aug 25 15:22:28 2018 |
committer | Georgios Valotasios <valotas@gmail.com> | Sat Aug 25 15:22:28 2018 |
tree | a84618c8c9c6433d10acdbda5686ad0debf0207b | |
parent | 6c53ee1843ed25e6662aaba0d130e231669382ba [diff] |
chore: prepare 3.0.0-dev.1.0
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.
In order to use the library, just add it to your pubspec.yaml
as a dependency.
For dart v1, you should be using the v2 of this package:
dependencies: mustache4dart: ">= 2.0.0 < 3.0.0"
For dart v2, you should be using the v3 of this package:
dependencies: mustache4dart: ">= 3.0.0 < 4.0.0"
and then import the package
import 'package:mustache4dart/mustache4dart.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!
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
[]
operator with firstname
as the parameterfirstname
firstname
firstname
(see Lambdas support)in each case the first valid value will be used.
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]'));
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!
The library passes all the optional lambda specs based on which lambdas must be treated 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.
In order to achive support on targets where dart:mirrors
is not allowed, reflectable
is being used. In such a case the user should mark the objects used by mustache4dart with @MustacheContext()
and make sure that the reflectable builder is being run accordingly.
reflectable
setupThe easiest way to get reflectable to work is by making use of build_runner
with a build.yaml
that should look like:
targets: # your package name, in my case is mustache4dart mustache4dart: builders: reflectable: # a list of dart files containing the annotated code, # in my case that is only some test files generate_for: - test/**_test.dart options: formatted: true
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
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
If you found a bug, just create a new issue or even better fork and issue a pull request with you fix.
The library will follow a semantic versioning