2024-10-13 01:45:27 +00:00
|
|
|
library protevus_mustache;
|
2016-04-22 02:22:33 +00:00
|
|
|
|
2021-06-20 12:37:20 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2024-10-13 01:45:27 +00:00
|
|
|
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
|
|
|
|
2024-10-12 10:35:14 +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));
|
2017-04-04 23:05:16 +00:00
|
|
|
|
2021-06-20 12:37:20 +00:00
|
|
|
var context =
|
|
|
|
MustacheContext(viewsDirectory, partialsDirectory, fileExtension);
|
2017-04-05 20:38:03 +00:00
|
|
|
|
2021-06-20 12:37:20 +00:00
|
|
|
var cache = MustacheViewCache(context);
|
2017-04-04 23:05:16 +00:00
|
|
|
|
2024-10-12 10:35:14 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
};
|
2017-04-05 20:38:03 +00:00
|
|
|
}
|