Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fe21e87
Showing
9 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
test: | ||
@./node_modules/.bin/mocha \ | ||
--reporter dot \ | ||
--bail | ||
|
||
.PHONY: test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
# co-render | ||
|
||
Template rendering for [co](https://github.com/visionmedia/co) using | ||
[co-render](https://github.com/visionmedia/co-render). This module | ||
provides higher level sugar than co-render to reduce redundancy, | ||
for example specifying a views directory and default extension name. | ||
|
||
## Installation | ||
|
||
``` | ||
$ npm install co-views | ||
``` | ||
|
||
And install whichever engine(s) you use: | ||
|
||
``` | ||
$ npm install ejs jade | ||
``` | ||
|
||
## Options | ||
|
||
- `map` an object mapping extnames to engine names [`{}`] | ||
- `ext` default esxtname to use when missing [`html`] | ||
|
||
### map | ||
|
||
For example if you wanted to use "swig" for .html files | ||
you would simply pass: | ||
|
||
```js | ||
{ map: { html: 'swig' } } | ||
``` | ||
|
||
### ext | ||
|
||
Set the default template extension when none is passed to | ||
the render function. This defaults to "html". For example | ||
if you mostly use Jade, then you'd likely want to assign | ||
this to: | ||
|
||
```js | ||
{ ext: 'jade' } | ||
``` | ||
|
||
Allowing you to invoke `render('user')` instead of | ||
`render('user.jade')`. | ||
|
||
## Example | ||
|
||
Render several users with different template engines in parallel: | ||
|
||
```js | ||
var co = require('co'); | ||
var views = require('co-views'); | ||
|
||
var render = views('examples', { | ||
map: { html: 'swig' } | ||
}); | ||
|
||
var tobi = { | ||
name: 'tobi', | ||
species: 'ferret' | ||
}; | ||
|
||
var loki = { | ||
name: 'loki', | ||
species: 'ferret' | ||
}; | ||
|
||
var luna = { | ||
name: 'luna', | ||
species: 'cat' | ||
}; | ||
|
||
co(function *(){ | ||
var a = render('user', { user: tobi }); | ||
var b = render('user.jade', { user: loki }); | ||
var c = render('user.ejs', { user: luna }); | ||
var html = yield [a, b, c]; | ||
html = html.join(''); | ||
console.log(html); | ||
}); | ||
``` | ||
|
||
# License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p><%= user.name %> is a <%= user.species %><p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>{{user.name}} is a {{user.species}}<p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
p #{user.name} is a #{user.species} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var co = require('co'); | ||
var views = require('..'); | ||
|
||
var render = views('examples', { | ||
map: { html: 'swig' } | ||
}); | ||
|
||
var tobi = { | ||
name: 'tobi', | ||
species: 'ferret' | ||
}; | ||
|
||
var loki = { | ||
name: 'loki', | ||
species: 'ferret' | ||
}; | ||
|
||
var luna = { | ||
name: 'luna', | ||
species: 'cat' | ||
}; | ||
|
||
co(function *(){ | ||
var a = render('user', { user: tobi }); | ||
var b = render('user.jade', { user: loki }); | ||
var c = render('user.ejs', { user: luna }); | ||
var html = yield [a, b, c]; | ||
html = html.join(''); | ||
console.log(html); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var debug = require('debug')('co-views'); | ||
var render = require('co-render'); | ||
var path = require('path'); | ||
var extname = path.extname; | ||
var join = path.join; | ||
|
||
/** | ||
* Pass views `dir` and `opts` to return | ||
* a render function. | ||
* | ||
* - `map` an object mapping extnames to engine names [{}] | ||
* - `ext` default extname to use when missing [html] | ||
* | ||
* @param {String} [dir] | ||
* @param {Object} [opts] | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
|
||
module.exports = function(dir, opts){ | ||
opts = opts || {}; | ||
|
||
debug('views %s %j', dir, opts); | ||
|
||
// view directory | ||
dir = dir || 'views'; | ||
|
||
// default extname | ||
var ext = opts.ext || 'html'; | ||
|
||
// engine map | ||
var map = opts.map || {}; | ||
|
||
return function(view, locals){ | ||
locals = locals || {}; | ||
|
||
// default extname | ||
var e = extname(view); | ||
|
||
if (!e) { | ||
e = '.' + ext;; | ||
view += e; | ||
} | ||
|
||
// remove leading '.' | ||
e = e.slice(1); | ||
|
||
// map engine | ||
locals.engine = map[e] || e; | ||
|
||
// resolve | ||
view = join(dir, view); | ||
|
||
debug('render %s %j', view, locals); | ||
return render(view, locals); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "co-view", | ||
"version": "0.0.1", | ||
"repository": "visionmedia/co-view", | ||
"description": "Higher level thunk-based template rendering for Co and others, built on co-render", | ||
"keywords": [ | ||
"template", | ||
"render", | ||
"consolidate", | ||
"engine", | ||
"koa" | ||
], | ||
"dependencies": { | ||
"debug": "*", | ||
"co-render": "0.0.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "*", | ||
"co": "~1.5.2", | ||
"ejs": "~0.8.4", | ||
"swig": "~1.0.0-rc1", | ||
"jade": "~0.35.0" | ||
}, | ||
"license": "MIT" | ||
} |