Async, partials
This commit is contained in:
parent
e1459c54f5
commit
6af38e0494
6 changed files with 35 additions and 7 deletions
|
@ -14,4 +14,9 @@ app.configure(mustache(new Directory('views')));
|
|||
|
||||
```
|
||||
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`.
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -2,7 +2,7 @@ name: angel_mustache
|
|||
description: Mustache view generator for Angel.
|
||||
author: thosakwe <thosakwe@gmail.com>
|
||||
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"
|
||||
|
|
|
@ -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!"));
|
||||
});
|
||||
}
|
1
test/partials/name.mustache
Normal file
1
test/partials/name.mustache
Normal file
|
@ -0,0 +1 @@
|
|||
world
|
1
test/with-partial.mustache
Normal file
1
test/with-partial.mustache
Normal file
|
@ -0,0 +1 @@
|
|||
Hello, {{> name}}!
|
Loading…
Reference in a new issue