Async, partials

This commit is contained in:
regiostech 2016-05-02 19:28:37 -04:00
parent e1459c54f5
commit 6af38e0494
6 changed files with 35 additions and 7 deletions

View file

@ -15,3 +15,8 @@ app.configure(mustache(new Directory('views')));
``` ```
res.render('hello', {'name': 'world'}); res.render('hello', {'name': 'world'});
``` ```
# 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`.

View file

@ -4,14 +4,30 @@ import 'dart:io';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:mustache4dart/mustache4dart.dart' show render; import 'package:mustache4dart/mustache4dart.dart' show render;
mustache(Directory viewsDirectory, {String fileExtension: '.mustache'}) { mustache(Directory viewsDirectory,
return (Angel app) { {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 { 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; String viewPath = name + fileExtension;
File viewFile = new File.fromUri( File viewFile = new File.fromUri(
viewsDirectory.absolute.uri.resolve(viewPath)); viewsDirectory.absolute.uri.resolve(viewPath));
if (await viewFile.exists()) { if (await viewFile.exists()) {
return render(await viewFile.readAsString(), data ?? {}); return render(await viewFile.readAsString(), data ?? {},
partial: partialsProvider);
} else throw new FileSystemException( } else throw new FileSystemException(
'View "$name" was not found.', viewPath); 'View "$name" was not found.', viewPath);
}; };

View file

@ -2,7 +2,7 @@ name: angel_mustache
description: Mustache view generator for Angel. description: Mustache view generator for Angel.
author: thosakwe <thosakwe@gmail.com> author: thosakwe <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_mustache homepage: https://github.com/angel-dart/angel_mustache
version: 1.0.0-dev version: 1.0.1
dependencies: dependencies:
angel_framework: ">=0.0.0-dev < 0.1.0" angel_framework: ">=0.0.0-dev < 0.1.0"
mustache4dart: ">= 1.0.0 < 2.0.0" mustache4dart: ">= 1.0.0 < 2.0.0"

View file

@ -4,9 +4,9 @@ import 'package:angel_framework/angel_framework.dart';
import 'package:angel_mustache/angel_mustache.dart'; import 'package:angel_mustache/angel_mustache.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
main() { main() async {
Angel angel = new Angel(); Angel angel = new Angel();
angel.configure(mustache(new Directory('/test'))); await angel.configure(mustache(new Directory('/test')));
test('can render templates', () async { test('can render templates', () async {
var hello = await angel.viewGenerator('hello', {'name': 'world'}); var hello = await angel.viewGenerator('hello', {'name': 'world'});
@ -24,4 +24,9 @@ main() {
print(fails); print(fails);
}), throws); }), throws);
}); });
test("partials", () async {
var withPartial = await angel.viewGenerator('with-partial');
expect(withPartial, equals("Hello, world!"));
});
} }

View file

@ -0,0 +1 @@
world

View file

@ -0,0 +1 @@
Hello, {{> name}}!