MustacheContext and fixed partials resolving

1. Partials are now resolved with the 'path' package
2. MustacheContext is an object that holds all folder contextual information for views and partials, and cleanly isolates all file resolving in one location
This commit is contained in:
Cory Forward 2017-04-05 14:38:03 -06:00
parent a3e5eab1a2
commit a6ae7844f7
3 changed files with 49 additions and 36 deletions

View file

@ -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);
};
};
}
}

View file

@ -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<String, String> viewCache = new HashMap();
HashMap<String, String> 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);
}
}

View file

@ -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}'));
}
}