platform/lib/angel_mustache.dart

35 lines
1.3 KiB
Dart
Raw Normal View History

2016-04-22 02:22:33 +00:00
library angel_mustache;
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:mustache4dart/mustache4dart.dart' show render;
2016-05-02 23:28:37 +00:00
mustache(Directory viewsDirectory,
{String fileExtension: '.mustache', String partialsPath: './partials'}) {
Directory partialsDirectory = new Directory.fromUri(
viewsDirectory.uri.resolve(partialsPath));
return (Angel app) async {
2016-04-22 02:22:33 +00:00
app.viewGenerator = (String name, [Map data]) async {
2016-05-02 23:28:37 +00:00
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);
};
2016-04-22 02:22:33 +00:00
String viewPath = name + fileExtension;
File viewFile = new File.fromUri(
viewsDirectory.absolute.uri.resolve(viewPath));
if (await viewFile.exists()) {
2016-05-02 23:28:37 +00:00
return render(await viewFile.readAsString(), data ?? {},
partial: partialsProvider);
2016-04-22 02:22:33 +00:00
} else throw new FileSystemException(
'View "$name" was not found.', viewPath);
};
};
}