This commit is contained in:
Tobe O 2017-11-18 13:39:10 -05:00
parent 09107d9e5b
commit a672d0df55
7 changed files with 41 additions and 42 deletions

View file

@ -1,6 +1,6 @@
# mustache # mustache
[![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/angel_dart/discussion) [![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/angel_dart/discussion)
[![version 1.0.3](https://img.shields.io/badge/pub-1.0.3-brightgreen.svg)](https://pub.dartlang.org/packages/angel_mustache) [![version](https://img.shields.io/pub/v/angel_mustache.svg)](https://pub.dartlang.org/packages/angel_mustache)
[![build status](https://travis-ci.org/angel-dart/mustache.svg?branch=master)](https://travis-ci.org/angel-dart/mustache) [![build status](https://travis-ci.org/angel-dart/mustache.svg?branch=master)](https://travis-ci.org/angel-dart/mustache)
Mustache (Handlebars) view generator for the [Angel](https://github.com/angel-dart/angel) Mustache (Handlebars) view generator for the [Angel](https://github.com/angel-dart/angel)
@ -13,19 +13,20 @@ In `pubspec.yaml`:
```yaml ```yaml
dependencies: dependencies:
angel_mustache: ^1.0.0 angel_mustache: ^1.1.0
``` ```
If your project imports [`package:angel_common`](https://github.com/angel-dart/common),
then this is already installed.
# Usage # Usage
```dart ```dart
// Run the plug-in const FileSystem fs = const LocalFileSystem();
await app.configure(mustache(new Directory('views')));
// Render `hello.mustache` configureServer(Angel app) async {
await res.render('hello', {'name': 'world'}); // Run the plug-in
await app.configure(mustache(fs.directory('views')));
// Render `hello.mustache`
await res.render('hello', {'name': 'world'});
}
``` ```
# Options # Options

2
analysis_options.yaml Normal file
View file

@ -0,0 +1,2 @@
analyzer:
strong-mode: true

View file

@ -1,32 +1,32 @@
library angel_mustache; library angel_mustache;
import 'dart:io';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:file/file.dart';
import 'package:mustache4dart/mustache4dart.dart' show render; import 'package:mustache4dart/mustache4dart.dart' show render;
import 'package:angel_mustache/src/cache.dart'; import 'package:path/path.dart' as p;
import 'package:angel_mustache/src/mustache_context.dart'; import 'src/cache.dart';
import 'package:path/path.dart' as path; import 'src/mustache_context.dart';
mustache(Directory viewsDirectory, mustache(Directory viewsDirectory,
{String fileExtension: '.mustache', String partialsPath: './partials'}) { {String fileExtension: '.mustache', String partialsPath: './partials'}) {
Directory partialsDirectory = Directory partialsDirectory = viewsDirectory.fileSystem
new Directory(path.join(path.fromUri(viewsDirectory.uri), partialsPath)); .directory(p.join(p.fromUri(viewsDirectory.uri), partialsPath));
MustacheContext context = MustacheContext context =
new MustacheContext(viewsDirectory, partialsDirectory, fileExtension); new MustacheContext(viewsDirectory, partialsDirectory, fileExtension);
MustacheCacheController cache = new MustacheCacheController(context); MustacheViewCache cache = new MustacheViewCache(context);
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.getPartialSync(name, app);
return render(template, data ?? {}, partial: partialsProvider); return render(template, data ?? {}, partial: partialsProvider);
}; };
String viewTemplate = await cache.get_view(name, app); String viewTemplate = await cache.getView(name, app);
return render(viewTemplate, data ?? {}, partial: partialsProvider); return await render(viewTemplate, data ?? {}, partial: partialsProvider);
}; };
}; };
} }

View file

@ -1,12 +1,11 @@
import 'dart:io'; import 'dart:async';
import 'dart:collection'; import 'dart:collection';
import 'package:path/path.dart' as path; import 'package:file/file.dart';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_mustache/src/mustache_context.dart'; import 'package:angel_mustache/src/mustache_context.dart';
class MustacheCacheController { class MustacheViewCache {
/** /**
* The context for which views and partials are * The context for which views and partials are
* served from. * served from.
@ -16,9 +15,9 @@ class MustacheCacheController {
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]); MustacheViewCache([this.context]);
get_view(String viewName, Angel app) async { Future<String> getView(String viewName, Angel app) async {
if (app.isProduction) { if (app.isProduction) {
if (viewCache.containsKey(viewName)) { if (viewCache.containsKey(viewName)) {
return viewCache[viewName]; return viewCache[viewName];
@ -27,7 +26,7 @@ class MustacheCacheController {
File viewFile = context.resolveView(viewName); File viewFile = context.resolveView(viewName);
if (await viewFile.exists()) { if (viewFile.existsSync()) {
String viewTemplate = await viewFile.readAsString(); String viewTemplate = await viewFile.readAsString();
if (app.isProduction) { if (app.isProduction) {
this.viewCache[viewName] = viewTemplate; this.viewCache[viewName] = viewTemplate;
@ -38,7 +37,7 @@ class MustacheCacheController {
'View "$viewName" was not found.', viewFile.path); 'View "$viewName" was not found.', viewFile.path);
} }
get_partial(String partialName, Angel app) { String getPartialSync(String partialName, Angel app) {
if (app.isProduction) { if (app.isProduction) {
if (partialCache.containsKey(partialName)) { if (partialCache.containsKey(partialName)) {
return partialCache[partialName]; return partialCache[partialName];

View file

@ -1,4 +1,4 @@
import 'dart:io'; import 'package:file/file.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
class MustacheContext { class MustacheContext {
@ -11,12 +11,10 @@ class MustacheContext {
MustacheContext([this.viewDirectory, this.partialDirectory, this.extension]); MustacheContext([this.viewDirectory, this.partialDirectory, this.extension]);
File resolveView(String viewName) { File resolveView(String viewName) {
return new File.fromUri( return viewDirectory.childFile('${viewName}${extension}');
viewDirectory.uri.resolve('${viewName}${extension}'));
} }
File resolvePartial(String partialName) { File resolvePartial(String partialName) {
return new File.fromUri( return partialDirectory.childFile('${partialName}${extension}');
partialDirectory.uri.resolve('${partialName}${extension}'));
} }
} }

View file

@ -2,9 +2,10 @@ name: angel_mustache
description: Mustache view generator for Angel. description: Mustache view generator for Angel.
author: thosakwe <thosakwe@gmail.com> author: thosakwe <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_mustache homepage: https://github.com/angel-dart/angel_mustache
version: 1.0.3 version: 1.1.0
dependencies: dependencies:
angel_framework: ">=1.0.0-dev < 2.0.0" angel_framework: "^1.1.0-alpha"
file: ^2.0.0
mustache4dart: ">= 1.0.0 < 2.0.0" mustache4dart: ">= 1.0.0 < 2.0.0"
dev_dependencies: dev_dependencies:
http: ">= 0.11.3 < 0.12.0" http: ">= 0.11.3 < 0.12.0"

View file

@ -1,12 +1,12 @@
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_mustache/angel_mustache.dart'; import 'package:angel_mustache/angel_mustache.dart';
import 'package:file/local.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
main() async { main() async {
Angel angel = new Angel(); Angel angel = new Angel();
await angel.configure(mustache(new Directory('./test'))); await angel.configure(mustache(const LocalFileSystem().directory('./test')));
test('can render templates', () async { test('can render templates', () async {
var hello = await angel.viewGenerator('hello', {'name': 'world'}); var hello = await angel.viewGenerator('hello', {'name': 'world'});
@ -17,12 +17,10 @@ main() async {
}); });
test('throws if view is not found', () { test('throws if view is not found', () {
expect( expect(new Future(() async {
new Future(() async { var fails = await angel.viewGenerator('fail', {'this_should': 'fail'});
var fails = await angel.viewGenerator( print(fails);
'fail', {'this_should': 'fail'}); }), throws);
print(fails);
}), throws);
}); });
test("partials", () async { test("partials", () async {