diff --git a/.gitignore b/.gitignore index 7c280441..f47e7629 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ build/ **/packages/ # Files created by dart2js -# (Most Dart developers will use pub build to compile Dart, use/modify these +# (Most Dart developers will use pub build to compile Dart, use/modify these # rules if you intend to use dart2js directly # Convention is to use extension '.dart.js' for Dart compiled to Javascript to # differentiate from explicit Javascript files) @@ -22,6 +22,8 @@ build/ # Directory created by dartdoc doc/api/ -# Don't commit pubspec lock file +# Don't commit pubspec lock file # (Library packages only! Remove pattern if developing an application package) pubspec.lock + +.idea \ No newline at end of file diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml new file mode 100644 index 00000000..5b99bf65 --- /dev/null +++ b/.idea/libraries/Dart_Packages.xml @@ -0,0 +1,363 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/All_Tests.xml b/.idea/runConfigurations/All_Tests.xml new file mode 100644 index 00000000..a824b209 --- /dev/null +++ b/.idea/runConfigurations/All_Tests.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index d2951a0b..8704f7d8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ # angel_hbs Handlebars view generator for Angel. + +# Installation +In `pubspec.yaml`: + + dependencies: + angel_mustache: ">= 1.0.0-dev < 2.0.0" + +# Usage +``` +app.configure(mustache(new Directory('views'))); +``` + +``` +res.render('hello', {'name': 'world'}); +``` \ No newline at end of file diff --git a/lib/angel_mustache.dart b/lib/angel_mustache.dart new file mode 100644 index 00000000..33caed50 --- /dev/null +++ b/lib/angel_mustache.dart @@ -0,0 +1,19 @@ +library angel_mustache; + +import 'dart:io'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:mustache4dart/mustache4dart.dart' show render; + +mustache(Directory viewsDirectory, {String fileExtension: '.mustache'}) { + return (Angel app) { + app.viewGenerator = (String name, [Map data]) async { + String viewPath = name + fileExtension; + File viewFile = new File.fromUri( + viewsDirectory.absolute.uri.resolve(viewPath)); + if (await viewFile.exists()) { + return render(await viewFile.readAsString(), data ?? {}); + } else throw new FileSystemException( + 'View "$name" was not found.', viewPath); + }; + }; +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 00000000..aa69ef87 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,11 @@ +name: angel_mustache +description: Mustache view generator for Angel. +author: thosakwe +homepage: https://github.com/angel-dart/angel_mustache +version: 1.0.0-dev +dependencies: + angel_framework: ">=0.0.0-dev < 0.1.0" + mustache4dart: ">= 1.0.0 < 2.0.0" +dev_dependencies: + http: ">= 0.11.3 < 0.12.0" + test: ">= 0.12.13 < 0.13.0" \ No newline at end of file diff --git a/test/all_tests.dart b/test/all_tests.dart new file mode 100644 index 00000000..2b87dcfb --- /dev/null +++ b/test/all_tests.dart @@ -0,0 +1,27 @@ +import 'dart:async'; +import 'dart:io'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_mustache/angel_mustache.dart'; +import 'package:test/test.dart'; + +main() { + Angel angel = new Angel(); + angel.configure(mustache(new Directory('/test'))); + + test('can render templates', () async { + var hello = await angel.viewGenerator('hello', {'name': 'world'}); + var bar = await angel.viewGenerator('foo/bar', {'framework': 'angel'}); + + expect(hello, equals("Hello, world!")); + expect(bar, equals("angel_framework")); + }); + + test('throws if view is not found', () { + expect( + new Future(() async { + var fails = await angel.viewGenerator( + 'fail', {'this_should': 'fail'}); + print(fails); + }), throws); + }); +} \ No newline at end of file diff --git a/test/foo/bar.mustache b/test/foo/bar.mustache new file mode 100644 index 00000000..d71ee58f --- /dev/null +++ b/test/foo/bar.mustache @@ -0,0 +1 @@ +{{framework}}_framework \ No newline at end of file diff --git a/test/hello.mustache b/test/hello.mustache new file mode 100644 index 00000000..e7773335 --- /dev/null +++ b/test/hello.mustache @@ -0,0 +1 @@ +Hello, {{name}}! \ No newline at end of file