How to: Use ejs without express

前端 未结 3 1363
猫巷女王i
猫巷女王i 2021-02-13 16:09

I\'m starting with node in general, and I\'m attempting to do a site without express. I would none the less want to use ejs to inject my html and this is where my problem is...

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-13 16:44

    There is a project called Consolidate.js which provides a common API for many template engines. This ensures they can all be interchangeable. If you want to render templates directly, you want to be compatible with this API.

    Sample code from the Consolidate.js README:

    var cons = require('consolidate');
    cons.swig('views/page.html', { user: 'tobi' }, function(err, html){
      if (err) throw err;
      console.log(html); // Or write to your `res` object here
    });
    

    This sample is for Swig, but similar code works for EJS or any of the compatible engines.

提交回复
热议问题