问题描述
通常,您直接渲染部分视图到响应对象:
response.partial('templatePath',{a:1,b:2,c:3} )
但是,由于我在一个Socket.io服务器事件中,我没有response对象。
有没有一种优雅的方法来使用响应对象将Jade部分视图呈现给不带的变量?
$ b
code> var jade = require('jade');
var template = require('fs')。readFileSync(pathToTemplate,'utf8');
var jadeFn = jade.compile(template,{filename:pathToTemplate,pretty:true});
var renderedTemplate = jadeFn({data:1,hello:'world'});
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:
response.partial('templatePath', {a:1, b:2, c:3})
However, since I'm inside a Socket.io server event, I don't have the "response" object.
Is there an elegant way to render a Jade partial-view to a variable without using the response object?
You can manually compile the Jade template.
var jade = require('jade');
var template = require('fs').readFileSync(pathToTemplate, 'utf8');
var jadeFn = jade.compile(template, { filename: pathToTemplate, pretty: true });
var renderedTemplate = jadeFn({data: 1, hello: 'world'});
这篇关于在Express.js中,如何在没有“响应”的情况下呈现Jade局部视图。目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!