Simple templating for koa using cheerio. You can use your jQuery knowledge for server-side templating.
npm install koa-cheerio
views/index.html
<html>
<body>
Hello <span id='hello'></span>!
</body>
</html>
var app = require('koa')();
var render = require('koa-cheerio');
app.use(render({ root: 'views/', ext: '.html' }));
app.use(function *index(next) {
var $ = yield this.render('index');
$('#hello').text('World');
});
app.listen(3000);
You can also pass an extra object if you need to modify any of the default parsing options:
var $ = yield this.render('index', {
normalizeWhitespace: true,
xmlMode: true
});
Comment from the cheerio-readme:
These parsing options are taken directly from htmlparser2, therefore any options that can be used in htmlparser2
are valid in cheerio as well. The default options are:
{
normalizeWhitespace: false,
xmlMode: false,
decodeEntities: true
}
MIT