[英]node js write html variable to jade template
I have a javascript
function in node js
that queries a database and returns a json
array to a callback function. 我在node js
中有一个javascript
函数,可以查询数据库并将json
数组返回给回调函数。 That callback function then formats that json
into a pure html
table and saves that information to a variable. 然后,该回调函数将该json
格式化为纯html
表,并将该信息保存到变量中。 What would be the best way to get that html table into the jade
template? 将那个html表放入jade
模板的最佳方法是什么? I want to render that HTML
table in the page. 我想在页面中呈现该HTML
表。
function getData() {
var sql = require("mssql");
var dbConfig={
server:"server",
database: "db",
user:"user",
password: "pw"
}
var conn = new sql.Connection(dbConfig);
var req = new sql.Request(conn);
conn.connect(function (err){
if (err) {
console.log(err);
return;
}
req.query("SELECT * FROM table",resultsCallback)
conn.close();
});
}
function resultsCallback (err, recordset) {
var tableify = require('tableify');
if (err) {
console.log(err);
}
else {
var html = tableify(recordset);
html = html.replace('<table>','');
html = html.replace('</table>',');
}
};
Note: You don't need to change the return value of tableify, you just need to use != instead of = look at link : Rendering HTML in variable using Jade 注意:您不需要更改tableify的返回值,只需要使用!=而不是=看看link: 使用Jade在变量中渲染HTML
While rendering your jade file, you can try passing the function in your route something like this: 渲染玉器文件时,您可以尝试在路由中传递如下功能:
-------- routes.js -------- -------- route.js --------
router.get ( '/' , function(req,res){
res.render(index,{
table: getData()
});
});
getData = function(){
...
}
-------- view.jade -------- -------- view.jade --------
!= table//rendering your table where table is of html format
!= will render the correct html, not a string !=将呈现正确的html,而不是字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.