gpt4 book ai didi

node.js - 在 Express.js 中,如何在没有 "response"对象的情况下渲染 Jade 局部 View ?

转载 作者:IT老高 更新时间:2023-10-28 23:07:05 26 4
gpt4 key购买 nike

使用 Express.js,我想将部分 View 从 Jade 模板渲染到变量。

通常,您将部分 View 直接渲染到响应对象:

response.partial('templatePath', {a:1, b:2, c:3})

但是,由于我在 Socket.io 服务器事件中,所以我没有“响应”对象。

有没有一种优雅的方法可以将 Jade 局部 View 渲染到变量而不使用响应对象?

最佳答案

这是 express 3 用户的直接解决方案(现在应该广泛传播):

res.partial() 已被删除,但如果响应对象不是当前上下文的一部分,例如 Liors 案例,您始终可以使用 app.render() 使用回调函数:

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
});

由于 app.render() 是 express app 对象的一个​​函数,它自然会知道配置的模板引擎和其他设置。它的行为方式与 app.get() 或其他快速请求事件上的特定 res.render() 相同。

另见:

关于node.js - 在 Express.js 中,如何在没有 "response"对象的情况下渲染 Jade 局部 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8644580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com