I need to render HAML templates in my Node.js/Express application. I tried to configure haml.js as views renderer:
haml = require('hamljs')
...
app.set('view engine', 'hamljs')
app.engine('.haml', haml.render)
And the code in my GET /
handeler:
options =
layout: "layout.haml"
locals:
message: 'world'
res.render('index.haml',options)
But application doesn't receive any data.
The is another example in haml.js documentation :
app.engine('.haml', require('hamljs').renderFile);
But there is no such function.
Setting up the haml templating engine did not work for me either. And as there are a few outdated pages out there on that topic, which could be a bit confusing, here is the 'manual way' which worked for me:
var haml = require('hamljs');
var fs = require('fs');
var express = require('express');
var app = express();
app.get('/', function(req, res) {
var hamlView = fs.readFileSync('views/home.haml', 'utf8');
res.end( haml.render(hamlView, {locals: {key: 'value'}) );
});
app.listen(process.env.PORT || 3000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.