platform/lib/angel_mustache.dart

33 lines
1.1 KiB
Dart
Raw Normal View History

2016-04-22 02:22:33 +00:00
library angel_mustache;
import 'package:angel_framework/angel_framework.dart';
2017-11-18 18:39:10 +00:00
import 'package:file/file.dart';
2016-04-22 02:22:33 +00:00
import 'package:mustache4dart/mustache4dart.dart' show render;
2017-11-18 18:39:10 +00:00
import 'package:path/path.dart' as p;
import 'src/cache.dart';
import 'src/mustache_context.dart';
2016-04-22 02:22:33 +00:00
2016-05-02 23:28:37 +00:00
mustache(Directory viewsDirectory,
{String fileExtension: '.mustache', String partialsPath: './partials'}) {
2017-11-18 18:39:10 +00:00
Directory partialsDirectory = viewsDirectory.fileSystem
.directory(p.join(p.fromUri(viewsDirectory.uri), partialsPath));
MustacheContext context =
new MustacheContext(viewsDirectory, partialsDirectory, fileExtension);
2017-11-18 18:39:10 +00:00
MustacheViewCache cache = new MustacheViewCache(context);
2016-05-02 23:28:37 +00:00
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) {
2017-11-18 18:39:10 +00:00
String template = cache.getPartialSync(name, app);
return render(template, data ?? {}, partial: partialsProvider);
2016-05-02 23:28:37 +00:00
};
2017-11-18 18:39:10 +00:00
String viewTemplate = await cache.getView(name, app);
return await render(viewTemplate, data ?? {}, partial: partialsProvider);
2016-04-22 02:22:33 +00:00
};
};
}