platform/packages/mustache/lib/protevus_mustache.dart

39 lines
1.3 KiB
Dart
Raw Normal View History

library protevus_mustache;
2016-04-22 02:22:33 +00:00
2021-06-20 12:37:20 +00:00
import 'dart:async';
import 'package:protevus_framework/protevus_framework.dart';
2017-11-18 18:39:10 +00:00
import 'package:file/file.dart';
2021-06-20 12:37:20 +00:00
import 'package:mustache_template/mustache_template.dart' as viewer;
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
Future Function(Protevus app) mustache(Directory viewsDirectory,
2021-06-20 12:37:20 +00:00
{String fileExtension = '.mustache', String partialsPath = './partials'}) {
var partialsDirectory = viewsDirectory.fileSystem
2017-11-18 18:39:10 +00:00
.directory(p.join(p.fromUri(viewsDirectory.uri), partialsPath));
2021-06-20 12:37:20 +00:00
var context =
MustacheContext(viewsDirectory, partialsDirectory, fileExtension);
2021-06-20 12:37:20 +00:00
var cache = MustacheViewCache(context);
return (Protevus app) async {
2021-06-20 12:37:20 +00:00
app.viewGenerator = (String name, [Map? data]) async {
//var partialsProvider;
2023-06-10 04:10:21 +00:00
partialsProvider(String name) {
2021-06-20 12:37:20 +00:00
var template = cache.getPartialSync(name, app)!;
//return render(template, data ?? {}, partial: partialsProvider);
return viewer.Template(template, name: name);
2023-06-10 04:10:21 +00:00
}
2016-05-02 23:28:37 +00:00
2021-06-20 12:37:20 +00:00
var viewTemplate = await (cache.getView(name, app));
//return await render(viewTemplate, data ?? {}, partial: partialsProvider);
var t = viewer.Template(viewTemplate ?? '',
partialResolver: partialsProvider);
return t.renderString(data ?? {});
2016-04-22 02:22:33 +00:00
};
};
}