diff --git a/lib/angel_mustache.dart b/lib/angel_mustache.dart index d26d8db1..18b5c486 100644 --- a/lib/angel_mustache.dart +++ b/lib/angel_mustache.dart @@ -4,27 +4,29 @@ import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; import 'package:mustache4dart/mustache4dart.dart' show render; import 'package:angel_mustache/src/cache.dart'; +import 'package:angel_mustache/src/mustache_context.dart'; +import 'package:path/path.dart' as path; mustache(Directory viewsDirectory, {String fileExtension: '.mustache', String partialsPath: './partials'}) { + Directory partialsDirectory = + new Directory(path.join(path.fromUri(viewsDirectory.uri), partialsPath)); - Directory partialsDirectory = new Directory.fromUri( - viewsDirectory.uri.resolve(partialsPath)); - - MustacheCacheController cache = new MustacheCacheController( - viewsDirectory, partialsDirectory, fileExtension); + MustacheContext context = + new MustacheContext(viewsDirectory, partialsDirectory, fileExtension); + + MustacheCacheController cache = new MustacheCacheController(context); return (Angel app) async { app.viewGenerator = (String name, [Map data]) async { var partialsProvider; partialsProvider = (String name) { - String template = cache.get_partial(name, app); - return render(template, data ?? {}, - partial: partialsProvider); + String template = cache.get_partial(name, app); + return render(template, data ?? {}, partial: partialsProvider); }; String viewTemplate = await cache.get_view(name, app); return render(viewTemplate, data ?? {}, partial: partialsProvider); }; }; -} \ No newline at end of file +} diff --git a/lib/src/cache.dart b/lib/src/cache.dart index 7d58336d..debafc81 100644 --- a/lib/src/cache.dart +++ b/lib/src/cache.dart @@ -1,29 +1,22 @@ import 'dart:io'; import 'dart:collection'; +import 'package:path/path.dart' as path; import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_mustache/src/mustache_context.dart'; class MustacheCacheController { + + /** + * The context for which views and partials are + * served from. + */ + MustacheContext context; + HashMap viewCache = new HashMap(); HashMap partialCache = new HashMap(); - /** - * The directory of the mustache views - */ - Directory viewDirectory; - - /** - * The directory of mustache partials - */ - Directory partialsDirectory; - - /** - * Default file extension associated with a view file - */ - String fileExtension; - - MustacheCacheController( - [this.viewDirectory, this.partialsDirectory, this.fileExtension]); + MustacheCacheController([this.context]); get_view(String viewName, Angel app) async { if (app.isProduction) { @@ -32,9 +25,7 @@ class MustacheCacheController { } } - String viewPath = viewName + this.fileExtension; - File viewFile = - new File.fromUri(this.viewDirectory.absolute.uri.resolve(viewPath)); + File viewFile = context.resolveView(viewName); if (await viewFile.exists()) { String viewTemplate = await viewFile.readAsString(); @@ -44,7 +35,7 @@ class MustacheCacheController { return viewTemplate; } else throw new FileSystemException( - 'View "$viewName" was not found.', viewPath); + 'View "$viewName" was not found.', viewFile.path); } get_partial(String partialName, Angel app) { @@ -54,18 +45,16 @@ class MustacheCacheController { } } - String viewPath = partialName + this.fileExtension; - File viewFile = - new File.fromUri(partialsDirectory.absolute.uri.resolve(viewPath)); + File partialFile = context.resolvePartial(partialName); - if (viewFile.existsSync()) { - String partialTemplate = viewFile.readAsStringSync(); + 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.', viewPath); + 'View "$partialName" was not found.', partialFile.path); } } diff --git a/lib/src/mustache_context.dart b/lib/src/mustache_context.dart new file mode 100644 index 00000000..d872e858 --- /dev/null +++ b/lib/src/mustache_context.dart @@ -0,0 +1,22 @@ +import 'dart:io'; +import 'package:path/path.dart' as path; + +class MustacheContext { + Directory viewDirectory; + + Directory partialDirectory; + + String extension; + + MustacheContext([this.viewDirectory, this.partialDirectory, this.extension]); + + File resolveView(String viewName) { + return new File.fromUri( + viewDirectory.uri.resolve('${viewName}${extension}')); + } + + File resolvePartial(String partialName) { + return new File.fromUri( + partialDirectory.uri.resolve('${partialName}${extension}')); + } +}