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
[![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)
Mustache (Handlebars) view generator for the [Angel](https://github.com/angel-dart/angel)
@ -13,19 +13,20 @@ In `pubspec.yaml`:
```yaml
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
```dart
// Run the plug-in
await app.configure(mustache(new Directory('views')));
const FileSystem fs = const LocalFileSystem();
// Render `hello.mustache`
await res.render('hello', {'name': 'world'});
configureServer(Angel app) async {
// Run the plug-in
await app.configure(mustache(fs.directory('views')));
// Render `hello.mustache`
await res.render('hello', {'name': 'world'});
}
```
# 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;
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:file/file.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;
import 'package:path/path.dart' as p;
import 'src/cache.dart';
import 'src/mustache_context.dart';
mustache(Directory viewsDirectory,
{String fileExtension: '.mustache', String partialsPath: './partials'}) {
Directory partialsDirectory =
new Directory(path.join(path.fromUri(viewsDirectory.uri), partialsPath));
Directory partialsDirectory = viewsDirectory.fileSystem
.directory(p.join(p.fromUri(viewsDirectory.uri), partialsPath));
MustacheContext context =
new MustacheContext(viewsDirectory, partialsDirectory, fileExtension);
MustacheCacheController cache = new MustacheCacheController(context);
MustacheViewCache cache = new MustacheViewCache(context);
return (Angel app) async {
app.viewGenerator = (String name, [Map data]) async {
var partialsProvider;
partialsProvider = (String name) {
String template = cache.get_partial(name, app);
String template = cache.getPartialSync(name, app);
return render(template, data ?? {}, partial: partialsProvider);
};
String viewTemplate = await cache.get_view(name, app);
return render(viewTemplate, data ?? {}, partial: partialsProvider);
String viewTemplate = await cache.getView(name, app);
return await render(viewTemplate, data ?? {}, partial: partialsProvider);
};
};
}

View file

@ -1,12 +1,11 @@
import 'dart:io';
import 'dart:async';
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_mustache/src/mustache_context.dart';
class MustacheCacheController {
class MustacheViewCache {
/**
* The context for which views and partials are
* served from.
@ -16,9 +15,9 @@ class MustacheCacheController {
HashMap<String, String> viewCache = 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 (viewCache.containsKey(viewName)) {
return viewCache[viewName];
@ -27,7 +26,7 @@ class MustacheCacheController {
File viewFile = context.resolveView(viewName);
if (await viewFile.exists()) {
if (viewFile.existsSync()) {
String viewTemplate = await viewFile.readAsString();
if (app.isProduction) {
this.viewCache[viewName] = viewTemplate;
@ -38,7 +37,7 @@ class MustacheCacheController {
'View "$viewName" was not found.', viewFile.path);
}
get_partial(String partialName, Angel app) {
String getPartialSync(String partialName, Angel app) {
if (app.isProduction) {
if (partialCache.containsKey(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;
class MustacheContext {
@ -11,12 +11,10 @@ class MustacheContext {
MustacheContext([this.viewDirectory, this.partialDirectory, this.extension]);
File resolveView(String viewName) {
return new File.fromUri(
viewDirectory.uri.resolve('${viewName}${extension}'));
return viewDirectory.childFile('${viewName}${extension}');
}
File resolvePartial(String partialName) {
return new File.fromUri(
partialDirectory.uri.resolve('${partialName}${extension}'));
return partialDirectory.childFile('${partialName}${extension}');
}
}

View file

@ -2,9 +2,10 @@ name: angel_mustache
description: Mustache view generator for Angel.
author: thosakwe <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_mustache
version: 1.0.3
version: 1.1.0
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"
dev_dependencies:
http: ">= 0.11.3 < 0.12.0"

View file

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