platform/packages/mustache/lib/angel_mustache.dart
Tobe O c94bec26ec Add 'packages/mustache/' from commit 'd0335a925d7199f3a5988ca10da36fee38ce0c43'
git-subtree-dir: packages/mustache
git-subtree-mainline: 9458a72c57
git-subtree-split: d0335a925d
2020-02-15 18:29:03 -05:00

32 lines
1.1 KiB
Dart

library angel_mustache;
import 'package:angel_framework/angel_framework.dart';
import 'package:file/file.dart';
import 'package:mustache4dart/mustache4dart.dart' show render;
import 'package:path/path.dart' as p;
import 'src/cache.dart';
import 'src/mustache_context.dart';
mustache(Directory viewsDirectory,
{String fileExtension: '.mustache', String partialsPath: './partials'}) {
Directory partialsDirectory = viewsDirectory.fileSystem
.directory(p.join(p.fromUri(viewsDirectory.uri), partialsPath));
MustacheContext context =
new MustacheContext(viewsDirectory, partialsDirectory, fileExtension);
MustacheViewCache cache = new MustacheViewCache(context);
return (Angel app) async {
app.viewGenerator = (String name, [Map data]) async {
var partialsProvider;
partialsProvider = (String name) {
String template = cache.getPartialSync(name, app);
return render(template, data ?? {}, partial: partialsProvider);
};
String viewTemplate = await cache.getView(name, app);
return await render(viewTemplate, data ?? {}, partial: partialsProvider);
};
};
}