platform/packages/mustache/lib/src/cache.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

59 lines
1.6 KiB
Dart

import 'dart:async';
import 'dart:collection';
import 'package:file/file.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_mustache/src/mustache_context.dart';
class MustacheViewCache {
/**
* The context for which views and partials are
* served from.
*/
MustacheContext context;
HashMap<String, String> viewCache = new HashMap();
HashMap<String, String> partialCache = new HashMap();
MustacheViewCache([this.context]);
Future<String> getView(String viewName, Angel app) async {
if (app.isProduction) {
if (viewCache.containsKey(viewName)) {
return viewCache[viewName];
}
}
File viewFile = context.resolveView(viewName);
if (viewFile.existsSync()) {
String viewTemplate = await viewFile.readAsString();
if (app.isProduction) {
this.viewCache[viewName] = viewTemplate;
}
return viewTemplate;
} else
throw new FileSystemException(
'View "$viewName" was not found.', viewFile.path);
}
String getPartialSync(String partialName, Angel app) {
if (app.isProduction) {
if (partialCache.containsKey(partialName)) {
return partialCache[partialName];
}
}
File partialFile = context.resolvePartial(partialName);
if (partialFile.existsSync()) {
String partialTemplate = partialFile.readAsStringSync();
if (app.isProduction) {
this.partialCache[partialName] = partialTemplate;
}
return partialTemplate;
} else
throw new FileSystemException(
'View "$partialName" was not found.', partialFile.path);
}
}