p-hatan/ mirror

pub repository

mirror.p-hatan.com

mustache4dart

2.1.1

A mustache implementation for the Dart language

Download archive

Readme

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:
  mustache4dart: '>= 2.0.0 < 3.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!

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

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

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add mustache4dart
pubspec.yaml
name: mustache4dart
version: 2.1.1
author: Georgios Valotasios <valotas@gmail.com>
description: A mustache implementation for the Dart language
homepage: https://github.com/valotas/mustache4dart
environment:
  sdk: '>=0.8.10+6 <2.0.0'
dev_dependencies:
  test: '>=0.12.0 <0.12.23'
  isolate: '>=1.0.0 <1.1.0'

Version history

VersionPublishedArchiveSHA-256
0.0.0-dev2013-02-19T21:28:29.445070ZDownloadd6d720bce05b079f2e943e7904c8bee9721b4056d116f7bd1c8403b1ec4a7f84
0.0.12013-02-26T22:03:46.448190ZDownload84179de91d6a4a697227726baba191627d8bb616ec1e4f837e81806f82de832b
0.0.32013-02-26T22:25:33.418970ZDownloade6c0658e0aaf82a90efbfb78d00ed32c72b1b75bf1cb5633e2c228466d4abc05
0.0.42013-03-03T18:06:55.789500ZDownloadeee0a90b0f4c2d99cb5231f241f5c39881fe864d5625283a2c58c0f158e570e0
0.0.72013-03-04T23:32:27.453030ZDownload355b01846a16fd00c64c8e323e428207c2e860637805df2747fc083d56735ba3
0.0.8+0.4.1.02013-03-07T21:36:57.679800ZDownload04dd1aaa4a3a78bb7493f1f7abbe268057ad3e2c69816bfa641faaa25a559e3b
0.0.9+0.4.1.02013-03-19T21:43:18.721950ZDownloadf76d8f4979877a6d2e387fa917ab99c8c68b9a03d01310682ccfa4538b262363
0.0.10+0.4.32013-03-29T09:16:43.510650ZDownload0a9daeefac6cede2b07e81a78bc6f11870439f8c0dc749c3006d7fa1613d3b41
0.0.10+0.4.5.12013-04-14T19:26:45.202910ZDownload1222f83daec4d3b8f857d587c96f90e45159caf31f3db553966459420da07664
0.0.11+0.4.7.12013-04-17T21:44:39.083260ZDownload689c53fb8889d842234e62544d500f15ac8669ee132d1ff1777f6f2eb367869a
0.0.12+0.5.0.12013-04-26T21:19:41.254860ZDownloadb62a950e075f5bb443b9feaadebcff537f111696285d8ea9493d4dcb8d7d050c
0.0.13+0.5.3.02013-05-06T06:44:50.599850ZDownloadd69524ea02b33bbb94230535a088711c5ab968e72e6ea7c62e8ce9a24f1c9140
0.0.14+0.5.7.32013-05-18T21:30:24.500820ZDownload68134df835b429a04397d6069c1ad0c378f29f1fccd91342d85c1b5eaf370bc7
0.0.16+0.5.9.02013-05-25T16:58:56.638630ZDownloadcf455f4ce40986b37714f377c0574099fef0203d1142a3c0f14a899c79dd3fa3
0.0.17+0.5.202013-06-24T19:46:16.406610ZDownloadc3352531ad3afa687b3125a92cc185541f4bf5a674f9b348f71d712bc5547de5
0.0.18+0.6.15.32013-08-08T16:10:34.844320ZDownload1fd4ff2ee68101a1b1d7b6ce865a901a1d04141bf3c805842a18dbfa1d13ecfe
0.0.19+0.6.21.32013-08-28T21:18:20.505260ZDownload624cc040cbc9b83ba56183b49167ab68cbd6e5949e40b6c0a43821c9358a3eea
0.0.20+0.6.21.32013-08-29T17:58:45.786730ZDownload418444b0cbe4824b31c8a96d98482248020bf3b274cacaa6f844fccbd6e72dc8
0.0.21+0.7.3.12013-09-21T17:05:15.179380ZDownload27a5cc9907374204518c4123264f4ec8b7fd27b5a257e2ad3e166d7ddd2c4dce
0.0.22+0.8.72013-10-28T23:09:03.918250ZDownload292858babcdd8fc42b69c7f29b23b91c1a54e277886a4778518535db2ded3a52
0.0.232013-11-09T12:16:04.708970ZDownloade3bf2c330dbc5cac99c80718644a35ea3d357399e360ae5e08ca7364d99e2272
0.0.242013-11-09T12:22:36.392200ZDownloaddcb83723c1a7b5984bb206933bb56e5979813816888e9e60219dd4d4e30f0092
1.0.02013-11-24T14:01:03.945520ZDownloadfbdf886fc5ce991323556aaf277d730c1837e56d301096b48db94eb701c50af1
1.0.12014-03-16T19:36:24.317110ZDownload3efe740572fcf904d7e48f13f1e6a283ed58fcae7b87a74eddd89c33f87cf2ad
1.0.22014-03-17T22:23:27.197490ZDownloadd104d41f1be20d36286ea4291bd39e8bc90288a10122819f0d1d9df7262a97a7
1.0.32014-04-03T21:28:20.126980ZDownloadb99045d7e4bc215d4301863e40956831dc8bc867eec36e1715be4fa3a906b115
1.0.42014-04-05T11:20:14.658ZDownload7db7a382ebbdd26196ce29b481e7932273982f14115f6b24404ccae0e0e724f0
1.0.52014-07-26T14:34:08.662790ZDownload076a8fe82009e7047fcbf814e46b4fde5365c5569fb1fd417c76ef3ee240e74f
1.0.62014-09-14T16:53:09.722960ZDownloadab87cb35322b90074e51f1e1b1790109e78a00a2cc5fb89a7231b7be5a6224dc
1.0.72014-10-23T20:13:40.391280ZDownload58f1e2bbfdcdf253b415c287ea0b9e1f39fcd58aea5d5ebcdc09352f245ae5af
1.0.82015-02-01T12:31:49.532440ZDownload38016c342ce69f773664695ec5a49bd0b57944eff6cc8e42ac0064b7f154aaa9
1.0.92015-02-13T19:53:49.499880ZDownload2095f1277ca308225852db7933b777301f50898fb9d857c65bf86e4649ed7c68
1.0.102015-03-15T16:48:32.234410ZDownload5aac935269c83772d0bc8c51c760d8d4ba5fbad699f21811226ea04b04c414f8
1.0.112017-03-17T18:14:57.650078ZDownloadbe0d21e99d2c4664814cca54be7e11be37e181681a1bbe0813da64d0ce5bd18d
1.0.122017-03-17T18:24:47.243489ZDownload625d1e3478edf1912dab43c055570257e50c41048cefb6915799fd6dcbbc0714
1.1.02017-05-10T20:30:22.023292ZDownloadab036d116ddca77d2af532d5f52783e1b0dc2f2bd8a2503ec9b74a621a455291
2.0.02017-06-13T19:23:50.361672ZDownload971eddc6f565af98b5408a01e507534bf3963a13dd18ad1c1826e07c0228506a
2.1.02017-06-21T20:42:44.818271ZDownload0472811dcb58ac047e349c9c62f02cb991188c02f24a19dd2c5c9597b8c17859
2.1.12018-04-14T21:18:28.022508ZDownloadf301b06380743067d387417253877366a86e2b2a7460011b7c5a8f3a101051c4
2.1.22018-05-12T17:16:39.618138ZDownloadf25d6f892ff79907ebe80da7bbe09a6610ee179b026419ee0abeabfe18c87811
3.0.0-dev.0.02018-08-19T15:44:53.264293ZDownloadc48f078a7add48b0e47fd0bcd7ae54df5a6fc2214c81b310a6caf5aefb1c7752
3.0.0-dev.1.02018-08-25T15:26:18.189409ZDownload8e300b762db7c2667e51554864606b8af2a5ef3a7f3075b4c7f613ca17962e3a

Dependencies

No direct dependencies declared.