koa-jade-render

1.0.0 • Public • Published

koa-jade-render

Jade for Koa with Spice.

Build Status Dependency Status License Status Downloads Version

Usage

npm install --save koa-jade-render

Note: I have not tested legacy but in theory it should work. Please report any bugs if encountered or create a pull request to add tests.

Legacy (< 2.0.0-alpha)

var koa = require('koa');
var app = koa();
var jade = require('koa-jade-render');
var path = require('path');
 
//set jade
app.use(jade(path.join(__dirname, 'views')));
 
//then render!
app.use(function*(next){
    yield this.render('index.jade');
});
 
app.listen(3000);

Modern (>= 2.0.0-alpha)

var Koa = require('koa');
var app = new Koa();
var jade = require('koa-jade-render');
var path = require('path');
 
//set jade
app.use(jade(path.join(__dirname, 'views')));
 
//then render!
app.use((self, next) => {
    return next().then(()=>{
        self.render('index.jade');
    });
});
 
app.listen(3000);

Note: You can still pass locals if you like:

Example:

app.use((self, next) => {
    return next().then(()=>{
        self.render('index.jade', {title:'BOOM!'});
    });
});
 
//the api is:
this.render(filename, opt, locals);
//note that opt and locals will be merged
//see example at https://github.com/jadejs/jade#api

Note: Jade uses jade.renderFile to render its views. If you would like override the renderer, you can create a plugin and pass it into Jade.

Example:

//...
 
//Jade takes two arguments, (opt, fn)
//where opt is a String, PlainObject, or Function
//and fn is a Function.
app.use(jade(function(file, opt, locals){
    //In this function, Koa's ctx is 
    //available at your disposal
    //Also for convenience, jade and 
    //options (global) are attached to ctx.
    var opt = this.jade.options;
    this.body = this.jade.compile(/*whatever you want to do*/);
    this.text = 'text/html';
}))
 
app.use((self, next) =>{
    //works as you would expect.
    next().then(()=>{
        this.render('file');
    });
});
 

Static API

jade.compile - see http://jade-lang.com/api/

jade.render - see http://jade-lang.com/api/

jade.renderFile - see http://jade-lang.com/api/

Why another Jade middleware for Koa?

Because this middleware supports koa's this.state and the code size is very small (~110 lines with beautiful comments!) so it's easier to maintain or contribute. Also, you're free to override or even create bad-to-the-bone plugins. Just name it something like koa-jade-render-MyAwesomePlugin.

Example

An example is available. See the example folder.

Contribute

You're more than welcome to contribute. This project isn't really a priority for me right now as I am working on another project where I am doing a major overhaul but if you would like to contribute, just do the standard Fork and Pull! Happy Coding!

Package Sidebar

Install

npm i koa-jade-render

Weekly Downloads

9

Version

1.0.0

License

MIT

Last publish

Collaborators

  • iwatakeshi