diff --git a/README.md b/README.md index 8704f7d8..26035bcd 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,9 @@ app.configure(mustache(new Directory('views'))); ``` res.render('hello', {'name': 'world'}); -``` \ No newline at end of file +``` + +# Options +- **partialsPath**: A path within the viewsDirectory to search for partials in. + Default is `./partials`. *Include the leading dot, please*. +- **fileExtension**: The file extension to search for. Default is `.mustache`. \ No newline at end of file diff --git a/lib/angel_mustache.dart b/lib/angel_mustache.dart index 33caed50..6688a2cd 100644 --- a/lib/angel_mustache.dart +++ b/lib/angel_mustache.dart @@ -4,14 +4,30 @@ 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) { +mustache(Directory viewsDirectory, + {String fileExtension: '.mustache', String partialsPath: './partials'}) { + Directory partialsDirectory = new Directory.fromUri( + viewsDirectory.uri.resolve(partialsPath)); + return (Angel app) async { app.viewGenerator = (String name, [Map data]) async { + var partialsProvider; + partialsProvider = (String name) { + String viewPath = name + fileExtension; + File viewFile = new File.fromUri( + partialsDirectory.absolute.uri.resolve(viewPath)); + if (viewFile.existsSync()) { + return render(viewFile.readAsStringSync(), data ?? {}, + partial: partialsProvider); + } else throw new FileSystemException( + 'View "$name" was not found.', viewPath); + }; + String viewPath = name + fileExtension; File viewFile = new File.fromUri( viewsDirectory.absolute.uri.resolve(viewPath)); if (await viewFile.exists()) { - return render(await viewFile.readAsString(), data ?? {}); + return render(await viewFile.readAsString(), data ?? {}, + partial: partialsProvider); } else throw new FileSystemException( 'View "$name" was not found.', viewPath); }; diff --git a/pubspec.yaml b/pubspec.yaml index aa69ef87..22265922 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: angel_mustache description: Mustache view generator for Angel. author: thosakwe homepage: https://github.com/angel-dart/angel_mustache -version: 1.0.0-dev +version: 1.0.1 dependencies: angel_framework: ">=0.0.0-dev < 0.1.0" mustache4dart: ">= 1.0.0 < 2.0.0" diff --git a/test/all_tests.dart b/test/all_tests.dart index 2b87dcfb..96619326 100644 --- a/test/all_tests.dart +++ b/test/all_tests.dart @@ -4,9 +4,9 @@ import 'package:angel_framework/angel_framework.dart'; import 'package:angel_mustache/angel_mustache.dart'; import 'package:test/test.dart'; -main() { +main() async { Angel angel = new Angel(); - angel.configure(mustache(new Directory('/test'))); + await angel.configure(mustache(new Directory('/test'))); test('can render templates', () async { var hello = await angel.viewGenerator('hello', {'name': 'world'}); @@ -24,4 +24,9 @@ main() { print(fails); }), throws); }); + + test("partials", () async { + var withPartial = await angel.viewGenerator('with-partial'); + expect(withPartial, equals("Hello, world!")); + }); } \ No newline at end of file diff --git a/test/partials/name.mustache b/test/partials/name.mustache new file mode 100644 index 00000000..04fea064 --- /dev/null +++ b/test/partials/name.mustache @@ -0,0 +1 @@ +world \ No newline at end of file diff --git a/test/with-partial.mustache b/test/with-partial.mustache new file mode 100644 index 00000000..74c0e738 --- /dev/null +++ b/test/with-partial.mustache @@ -0,0 +1 @@ +Hello, {{> name}}! \ No newline at end of file