1

Is there a template engine for nodejs like razorengine which is specialized in outputting html but not limited to html. So I would be able to create javascript files on the fly with it too like i can with RazorEngine?

An example i would love to be able to do:

var fs = require('fs');
var engine = require('templatingEngine');

var template = "<p>Hello, <% name %></p>";
var data = [
    {id:"1", name: "bob"},
    {id:"2", name: "pete"},
    {id:"3", name: "jake"}
    ];

var result = engine.parse(template, data);

fs.writeFile("/tmp/hellos.html", result, function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
});

hellos.html ==>

<p>Hello, bob</p>
<p>Hello, pete</p>
<p>Hello, jake</p>
wendellmva
  • 1,896
  • 1
  • 26
  • 33
  • The differentiation here is: I don't want to use it for just html, I want to be able to generate anything script based not just html and the engine should be decoupled from sending a http response to the client... just like the razorengine is ... – wendellmva Sep 27 '13 at 20:21

3 Answers3

2

In generally, only a few view engines, like Jade, are targeted largely at generating markup. Most will use some form of embedded statements -- similar to Razor's @..., such as mustache's {{...}} -- to support templating any desired text content.

And, usually any coupling to the request/response will be created by a part of an http framework. With Express, for example (ref: app settings, app.engine()):

app.set('view engine', 'jade');
app.engine('html', require('ejs').renderFile);

The exact API may vary with from one view engine to another. But, with Handlebars, you could do:

var handlebars = require('handlebars');

var template = "{{#each this}}<p>Hello, {{name}}</p>{{/each}}";
var compiledTemplate = handlebars.compile(template);

var data = [
    {id:"1", name: "bob"},
    {id:"2", name: "pete"},
    {id:"3", name: "jake"}
    ];

var result = compiledTemplate(data);

And, for a sampling of view engines, the consolidate project lists around 20.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
2

Yes there is and its called razorjs and is available on github

wendellmva
  • 1,896
  • 1
  • 26
  • 33
0

You have already AngularJS(and other javascript based engines) , you can render the data in angularJS then push it to httpresponse . it is designed for client side , but with some teaks it can work on javascript serverside too

sino
  • 754
  • 1
  • 7
  • 22
  • Yes thanks for that but I don't think that will work on anything other than html and besides I want to store the results not send it to the client. – wendellmva Sep 27 '13 at 20:17
  • you can store them , it doesnt send them to client automatically – sino Sep 27 '13 at 20:33
  • I have to admit that sounds good since then i could include angular directives and filters in my templates without having to roll a preprocessor language of my own (since i was thinking of writing my own engine). Could you point me into a direction where to start. I use angular but have no clue how it works under the hood? – wendellmva Sep 27 '13 at 21:00