Express templating engine with html and plain text templates?

353 views Asked by At

I'm looking for a way to respond either plain text and html (and json) from an ExpressJS app (depending on the "Accept" header). In all cases I need some way to pass the output through a templating engine before the actual output. Therefore I'm looking for a templating engine which supports that right away.

So far I'm using a workaround with Swig like this:

 var app = express();     
 var swig = require('swig');

 app.use(function (req, res, next) {
     if (req.accepts('text/html')) {
         app.engine('html', swig.renderFile);
         app.set('view engine', 'html');
         app.set('views', path.join(__dirname, 'views/html'));
     } else {
         app.engine('txt', swig.renderFile);
         app.set('view engine', 'txt');
         res.set('Content-Type', 'text/plain');
         app.set('views', path.join(__dirname, 'views/text'));
     }
     next();
 });

But I assume there's a better/native way to do that, already implemented somewhere?

Cheers

0

There are 0 answers