Merge pull request #4 from c4wrd/master

Introduced MustacheContext and fixed partials resolving
This commit is contained in:
Tobe O 2017-04-05 18:40:23 -04:00 committed by GitHub
commit db83aef08f
3 changed files with 49 additions and 36 deletions

View file

@ -4,23 +4,25 @@ import 'dart:io';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:mustache4dart/mustache4dart.dart' show render; import 'package:mustache4dart/mustache4dart.dart' show render;
import 'package:angel_mustache/src/cache.dart'; 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, mustache(Directory viewsDirectory,
{String fileExtension: '.mustache', String partialsPath: './partials'}) { {String fileExtension: '.mustache', String partialsPath: './partials'}) {
Directory partialsDirectory =
new Directory(path.join(path.fromUri(viewsDirectory.uri), partialsPath));
Directory partialsDirectory = new Directory.fromUri( MustacheContext context =
viewsDirectory.uri.resolve(partialsPath)); new MustacheContext(viewsDirectory, partialsDirectory, fileExtension);
MustacheCacheController cache = new MustacheCacheController( MustacheCacheController cache = new MustacheCacheController(context);
viewsDirectory, partialsDirectory, fileExtension);
return (Angel app) async { return (Angel app) async {
app.viewGenerator = (String name, [Map data]) async { app.viewGenerator = (String name, [Map data]) async {
var partialsProvider; var partialsProvider;
partialsProvider = (String name) { partialsProvider = (String name) {
String template = cache.get_partial(name, app); String template = cache.get_partial(name, app);
return render(template, data ?? {}, return render(template, data ?? {}, partial: partialsProvider);
partial: partialsProvider);
}; };
String viewTemplate = await cache.get_view(name, app); String viewTemplate = await cache.get_view(name, app);

View file

@ -1,29 +1,22 @@
import 'dart:io'; import 'dart:io';
import 'dart:collection'; import 'dart:collection';
import 'package:path/path.dart' as path;
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_mustache/src/mustache_context.dart';
class MustacheCacheController { class MustacheCacheController {
/**
* The context for which views and partials are
* served from.
*/
MustacheContext context;
HashMap<String, String> viewCache = new HashMap(); HashMap<String, String> viewCache = new HashMap();
HashMap<String, String> partialCache = new HashMap(); HashMap<String, String> partialCache = new HashMap();
/** MustacheCacheController([this.context]);
* 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]);
get_view(String viewName, Angel app) async { get_view(String viewName, Angel app) async {
if (app.isProduction) { if (app.isProduction) {
@ -32,9 +25,7 @@ class MustacheCacheController {
} }
} }
String viewPath = viewName + this.fileExtension; File viewFile = context.resolveView(viewName);
File viewFile =
new File.fromUri(this.viewDirectory.absolute.uri.resolve(viewPath));
if (await viewFile.exists()) { if (await viewFile.exists()) {
String viewTemplate = await viewFile.readAsString(); String viewTemplate = await viewFile.readAsString();
@ -44,7 +35,7 @@ class MustacheCacheController {
return viewTemplate; return viewTemplate;
} else } else
throw new FileSystemException( throw new FileSystemException(
'View "$viewName" was not found.', viewPath); 'View "$viewName" was not found.', viewFile.path);
} }
get_partial(String partialName, Angel app) { get_partial(String partialName, Angel app) {
@ -54,18 +45,16 @@ class MustacheCacheController {
} }
} }
String viewPath = partialName + this.fileExtension; File partialFile = context.resolvePartial(partialName);
File viewFile =
new File.fromUri(partialsDirectory.absolute.uri.resolve(viewPath));
if (viewFile.existsSync()) { if (partialFile.existsSync()) {
String partialTemplate = viewFile.readAsStringSync(); String partialTemplate = partialFile.readAsStringSync();
if (app.isProduction) { if (app.isProduction) {
this.partialCache[partialName] = partialTemplate; this.partialCache[partialName] = partialTemplate;
} }
return partialTemplate; return partialTemplate;
} else } else
throw new FileSystemException( 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}'));
}
}