Search code examples
node.jsexpressdust.js

Express Render Template from String


When a GET request is made on my site, I am trying to render templates that are stored on another server. Essentially my application will make a request to this server, and the response will contain the template that I would like to use. The server's response is a dust template in the form of a string.

What is the best way to render the template?

router.get('/', function(req, res) {
   var options = {
   host: myHost,
   path: myPath,
   port: myPort};

   var templateReq = http.get(options, function(response) {
      var templateStr = '';

      response.on('data', function(chunk) {
         templateStr += chunk;
      });

      response.on('end', function() {
         var dustParams = myDustParams;
         res.render(templateStr, dustParams); //THIS DOES NOT WORK
      });
   });
});

I understand that render looks in the 'views' folder to search for a template.

Also, I know that you can directly send HTML to the response using 'send', but by doing this I am unable to include the dust parameters.


Solution

  • You can use:

    res.send(templateStr);
    

    Regarding having dust populate the params in the template, you have to compile and render it before using the res.send