express-partials
Advanced tools
Comparing version 0.0.3 to 0.0.4
0.0.4 / 2012-06-25 | ||
================== | ||
* Improved `partials.register()`. (thanks to Stéphane Alnet) | ||
0.0.3 / 2012-06-25 | ||
@@ -3,0 +9,0 @@ ================== |
17
index.js
@@ -20,2 +20,5 @@ var path = require('path') | ||
* app.use(partials()); | ||
* // three ways to register a template engine: | ||
* partials.register('coffee','coffeekup'); | ||
* partials.register('coffee',require('coffeekup')); | ||
* partials.register('coffee',require('coffeekup').render); | ||
@@ -79,2 +82,7 @@ * app.get('/',function(req,res,next){ | ||
* (Similar to Express 2.x register() function.) | ||
* | ||
* The second argument might be: | ||
* a template module's name | ||
* a module with a `render` method | ||
* a synchronous `render` method | ||
*/ | ||
@@ -86,3 +94,10 @@ | ||
} | ||
register[ext] = render; | ||
if(typeof render === 'string') { | ||
render = require(render); | ||
} | ||
if(render.render !== null) { | ||
register[ext] = render.render; | ||
} else { | ||
register[ext] = render; | ||
} | ||
}; | ||
@@ -89,0 +104,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "Express 3.x Layout & Partial support.", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"repository": { | ||
@@ -20,3 +20,6 @@ "url": "https://github.com/publicclass/express-partials" | ||
"ejs": "*", | ||
"jade": "*" | ||
"jade": "*", | ||
"eco": "*", | ||
"coffeecup": "*", | ||
"consolidate": "*" | ||
}, | ||
@@ -23,0 +26,0 @@ "scripts": { |
@@ -14,39 +14,50 @@ # express-partials | ||
## Usage | ||
The simple case: | ||
```javascript | ||
var express = require('express') | ||
, partials = require('express-partials') | ||
, app = express(); | ||
app.use(partials()); | ||
var express = require('express') | ||
, partials = require('express-partials') | ||
, app = express(); | ||
// optionally register a template engine (defaults to ejs) | ||
partials.register('.jade',require('jade').render); | ||
app.get('/',function(req,res,next){ | ||
res.render('index.ejs') | ||
// -> render layout.ejs with index.ejs as `body`. | ||
}) | ||
app.get('/no-layout',function(req,res,next){ | ||
res.render('index.ejs',{layout:false}) | ||
// -> only renders index.ejs | ||
}) | ||
// load the express-partials middleware | ||
app.use(partials()); | ||
app.get('/mobile',function(req,res,next){ | ||
res.render('index.ejs',{layout:'mobile'}) | ||
// -> render mobile.ejs with index.ejs as `body`. | ||
}) | ||
app.get('/',function(req,res,next){ | ||
res.render('index.ejs') | ||
// -> render layout.ejs with index.ejs as `body`. | ||
}) | ||
app.get('/no-layout',function(req,res,next){ | ||
res.render('index.ejs',{layout:false}) | ||
// -> only renders index.ejs | ||
}) | ||
app.get('/mobile',function(req,res,next){ | ||
res.render('index.ejs',{layout:'mobile'}) | ||
// -> render mobile.ejs with index.ejs as `body`. | ||
}) | ||
``` | ||
By default express-partials tries to figure the engine out by using the extension of the template. But in special cases an extension can be registered. And this can be done in a few ways: | ||
## Template Support (tested) | ||
```javascript | ||
// a function | ||
partials.register('.j',require('jade').render); | ||
- [ejs](https://github.com/visionmedia/ejs) | ||
- [jade](https://github.com/visionmedia/jade) | ||
// module (or object with a .render() function) | ||
partials.register('.j',require('jade')); | ||
// string (= require(str)) | ||
partials.register('.j','jade'); | ||
``` | ||
## Template Support | ||
Any synchronous template engine should work fine. But check out the [tests](./test/test.partials.js) for a few engines tested. | ||
## TODO | ||
- More Tests! | ||
- More template engines. | ||
- Async template engines? | ||
@@ -56,5 +67,5 @@ | ||
To run the test suite first invoke the following command within the repo, installing the development dependencies: | ||
To run the test suite first install dependencies with the following command within the repo: | ||
$ npm install -d | ||
$ npm install | ||
@@ -61,0 +72,0 @@ then run the tests: |
@@ -45,3 +45,4 @@ var express = require('express') | ||
partials.register('.j',require('jade').render); | ||
/* Use `register` to substitute the file extension. */ | ||
app.engine('.j',require('jade').__express); | ||
app.get('/register/no-layout',function(req,res,next){ | ||
@@ -51,3 +52,2 @@ res.render('index.j',{hello:'world',layout:false}) | ||
app.engine('.j',require('jade').__express); | ||
app.get('/register',function(req,res,next){ | ||
@@ -57,2 +57,23 @@ res.render('index.j',{hello:'world'}) | ||
/* Eco is supported by consolidate. Tell Express 3.x | ||
* to use that. | ||
* express-partials uses Eco's render() automatically. | ||
*/ | ||
app.engine('.eco',require('consolidate').eco); | ||
app.get('/eco',function(req,res,next){ | ||
res.render('index.eco',{hello:'world'}) | ||
}) | ||
/* CoffeeCup doesn't support Express 3.x yet, and isn't | ||
* handled by consolidate either. We provide our own | ||
* renderFile() implementation. | ||
* express-partials uses CoffeeCup's render() automatically. | ||
*/ | ||
app.engine('.coffeecup',function(path,options,callback) { | ||
callback(null,require('coffeecup').render(require('fs').readFileSync(path,'utf8'),options)); | ||
}); | ||
app.get('/coffeecup',function(req,res,next){ | ||
res.render('index.coffeecup',{hello:'world'}) | ||
}) | ||
describe('app',function(){ | ||
@@ -155,9 +176,10 @@ describe('GET /',function(){ | ||
describe('GET /register/no-layout',function(){ | ||
it('should render index.j as a Jade template',function(done){ | ||
describe('GET /register',function(){ | ||
it('should render index.j as a Jade template with layout.j as Jade layout (register: function)',function(done){ | ||
partials.register('.j',require('jade').render); | ||
request(app) | ||
.get('/register/no-layout') | ||
.get('/register') | ||
.end(function(res){ | ||
res.should.have.status(200); | ||
res.body.should.equal('<h2>Jade says hello world</h2>'); | ||
res.body.should.equal('<html><head><title>Jade layout</title></head><body><h2>Jade says hello world</h2></body></html>'); | ||
done(); | ||
@@ -169,3 +191,4 @@ }) | ||
describe('GET /register',function(){ | ||
it('should render index.j as a Jade template with layout.j as Jade layout',function(done){ | ||
it('should render index.j as a Jade template with layout.j as Jade layout (register: module)',function(done){ | ||
partials.register('.j',require('jade')); | ||
request(app) | ||
@@ -181,2 +204,52 @@ .get('/register') | ||
describe('GET /register',function(){ | ||
it('should render index.j as a Jade template with layout.j as Jade layout (register: name)',function(done){ | ||
partials.register('.j','jade'); | ||
request(app) | ||
.get('/register') | ||
.end(function(res){ | ||
res.should.have.status(200); | ||
res.body.should.equal('<html><head><title>Jade layout</title></head><body><h2>Jade says hello world</h2></body></html>'); | ||
done(); | ||
}) | ||
}) | ||
}) | ||
describe('GET /register/no-layout',function(){ | ||
it('should render index.j as a Jade template (using only Express 3.x)',function(done){ | ||
partials.register('.j',{}); | ||
request(app) | ||
.get('/register/no-layout') | ||
.end(function(res){ | ||
res.should.have.status(200); | ||
res.body.should.equal('<h2>Jade says hello world</h2>'); | ||
done(); | ||
}) | ||
}) | ||
}) | ||
describe('GET /eco',function(){ | ||
it('should render index.eco as a Eco template with layout.eco as Eco layout',function(done){ | ||
request(app) | ||
.get('/eco') | ||
.end(function(res){ | ||
res.should.have.status(200); | ||
res.body.should.equal('<html><head><title>Eco layout</title></head><body><h2>Eco says hello world</h2>\n</body></html>\n'); | ||
done(); | ||
}) | ||
}) | ||
}) | ||
describe('GET /coffeecup',function(){ | ||
it('should render index.coffeecup as a CoffeeCup template with layout.coffeecup as CoffeeCup layout',function(done){ | ||
request(app) | ||
.get('/coffeecup') | ||
.end(function(res){ | ||
res.should.have.status(200); | ||
res.body.should.equal('<html><head><title>CoffeeCup layout</title></head><body><h2>CoffeeCup says hello world</h2></body></html>'); | ||
done(); | ||
}) | ||
}) | ||
}) | ||
}) |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
21834
19
584
98
8
5