Reputation: 855
I have a jade file called syntax.jade. What I need is a way to convert syntax.jade, along with the information I would normally pass it with a res.render statement, into a html document in node so that I can manipulate it in that form. Basically what I want is this:
when I use
res.render('syntax', {comment: comment}, function(err, html))
html contains the html of the page as a string. I need another way to get that code that doesn't require me to render the page in the browser.
The reason I want to do this is so that I can include the resulting HTML code in another res.render statement in order to provide formatting instead of doing all the formatting in the front end.
Upvotes: 1
Views: 405
Reputation: 91609
You can just require Jade as a Node module and use the renderFile()
method.
var jade = require('jade');
jade.renderFile('syntax.jade', {comment: comment}), function (err, html) {
if (err) throw err;
// rendered string is in html variable
});
If there's no error, then you have a rendered HTML string as a result. If you want to do this synchronously, then just don't specify a callback:
var html = jade.renderFile('filename.jade', {comment: comment});
Upvotes: 2
Reputation: 145994
That is already what you have. From the express docs on res.render
When a callback is provided both the possible error and rendered string are passed, and no automated response is performed.
So res.render(viewName, locals)
does BOTH rendering of the HTML and sending that HTML as the HTTP response body. However res.render(viewName, locals, callback)
with 3 arguments just renders the HTML and passes it to the callback without sending any HTTP response. Thus you can do whatever is needed with the HTML and then send a response later.
There is also app.render
which is another general utility to render stuff without having anything to do with a particular http request/response.
Upvotes: 1