In Express.js, how can I render a Jade partial-view without a “response” object?

前端 未结 2 1814
深忆病人
深忆病人 2020-12-28 19:47

Using Express.js, I\'d like to render a partial-view from a Jade template to a variable.

Usually, you render a partial-view directly to the response object:

2条回答
  •  别那么骄傲
    2020-12-28 20:11

    Here's the straight solution to this problem for express 3 users (which should be widely spread now):

    res.partial() has been removed but you can always use app.render() using the callback function, if the response object is not part of the current context like in Liors case:

    app.render('templatePath', {
      a: 1,
      b: 2,
      c: 3
    },function(err,html) {
      console.log('html',html);
      // your handling of the rendered html output goes here
    });
    

    Since app.render() is a function of the express app object it's naturally aware of the configured template engine and other settings. It behaves the same way as the specific res.render() on app.get() or other express request events.

    See also:

    • http://expressjs.com/api.html#app.render for app.render()
    • https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x for express 2.x > 3.x migration purposes

提交回复
热议问题