How to display a HAML template using haml.js in Express

I need to display HAML templates in my Node.js / Express application. I tried to configure haml.js as view 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 the application does not receive any data.

This is another example in the haml.js documentation :

 app.engine('.haml', require('hamljs').renderFile); 

But there is no such function.

+4
source share
1 answer

Setting up the haml template engine also didn't work for me. And since there are some outdated pages in this thread, which can be a bit confusing, here is the “manual way” that 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); 
+1
source

Source: https://habr.com/ru/post/1502796/


All Articles