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;
|
2017-04-04 23:05:16 +00:00
|
|
|
import 'package:angel_mustache/src/cache.dart';
|
2017-04-05 20:38:03 +00:00
|
|
|
import 'package:angel_mustache/src/mustache_context.dart';
|
|
|
|
import 'package:path/path.dart' as path;
|
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-04-05 20:38:03 +00:00
|
|
|
Directory partialsDirectory =
|
|
|
|
new Directory(path.join(path.fromUri(viewsDirectory.uri), partialsPath));
|
2017-04-04 23:05:16 +00:00
|
|
|
|
2017-04-05 20:38:03 +00:00
|
|
|
MustacheContext context =
|
|
|
|
new MustacheContext(viewsDirectory, partialsDirectory, fileExtension);
|
|
|
|
|
|
|
|
MustacheCacheController cache = new MustacheCacheController(context);
|
2017-04-04 23:05:16 +00:00
|
|
|
|
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-04-05 20:38:03 +00:00
|
|
|
String template = cache.get_partial(name, app);
|
|
|
|
return render(template, data ?? {}, partial: partialsProvider);
|
2016-05-02 23:28:37 +00:00
|
|
|
};
|
|
|
|
|
2017-04-04 23:05:16 +00:00
|
|
|
String viewTemplate = await cache.get_view(name, app);
|
|
|
|
return render(viewTemplate, data ?? {}, partial: partialsProvider);
|
2016-04-22 02:22:33 +00:00
|
|
|
};
|
|
|
|
};
|
2017-04-05 20:38:03 +00:00
|
|
|
}
|