Jehanzeb.Malik
Jehanzeb.Malik

Reputation: 3390

Compile html from templates on nodejs

My issue here is that I want to compile html templates on nodejs server. We want to make dynamic templates, where we can compile templates + insert data into templates using JSON object.

I tried using node's package "Mustache", and I am getting what I mentioned above. But the issue with Mustache is that the html is written in script tag in *.html file. To compile I have to read it as a file stream into a variable and use it. And I don't want to do that as I previously had issues with fs.

So my question is that what other node's module can I use where I can design complete HTML templates and then compile them using the data from JSON objects.

Upvotes: 0

Views: 2159

Answers (1)

Max
Max

Reputation: 2836

Try out hogan-express. It's basically moustach(javascript version) which is better than the default hjs module that express installs on start.

You can send data to the template using variables {{variableName}}

This variable is set on your backend with the following(I'm assuming you are using express)

var value = "dynamicValue";

app.get('/', function(req, res) {
    res.render('index.html', {
        variableName: value
    });
});

In your template you simply call {{ variableName }}

Note, this needs to be in you app.js

app.engine('html', require('hogan-express'));

As for other options you can use jade, ejs, etc... I personally prefer hogan

Upvotes: 1

Related Questions