我使用带有EJS的Express.js将数据库查询的响应呈现为html无序列表。我也使用json2html。我没有收到任何错误,但我的列表项元素没有呈现为HTML。我做错了什么?
该页面在我的浏览器中呈现如下 -
My Express路由器处理程序 -
var $stmt = squel.select().from('GrpPractice').field('*').toString();
router.get('/', handler1);
function handler1(req, res) {
db.all($stmt, function(err, rows) {
var transform = {'tag':'li', 'html':'${Organization legal name}'};
var html = json2html.transform(rows, transform);
res.render('index', { title: 'Group Practices', html });
// res.send(rows);
});
}
我的EJS模板 -
<body>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
<ul>
<%= html %>
</ul>
</body>
我的数据显示良好 -
[{"Organization legal name":"23RD FAMILY MED LLC","Group Practice PAC ID":"3375715477","Number of Group Practice members":5,"First Name":"CHERL","Last Name":"MUES","Credential":"","Primary specialty":"NURSE PRACTITIONER"},{"Organization legal name":"23RD FAMILY MED LLC","Group Practice PAC ID":"3375715477","Number of Group Practice members":5,"First Name":"PAUL","Last Name":"GLOWACKI","Credential":"MD","Primary specialty":"FAMILY PRACTICE"},{"Organization legal name":"23RD FAMILY MED LLC","Group Practice PAC ID":"3375715477","Number of Group Practice members":5,"First Name":"MONTY","Last Name":"SELLON","Credential":"","Primary specialty":"FAMILY PRACTICE"},{"Organization legal name":"23RD FAMILY MED LLC","Group Practice PAC ID":"3375715477","Number of Group Practice members":5,"First Name":"MILO","Last Name":"ANDERSON","Credential":"","Primary specialty":"FAMILY PRACTICE"},{"Organization legal name":"23RD FAMILY MED LLC","Group Practice PAC ID":"3375715477","Number of Group Practice members":5,"First Name":"ANDREW","Last Name":"OPP","Credential":"","Primary specialty":"FAMILY PRACTICE"},{"Organization legal name":"3-D OPTICAL","Group Practice PAC ID":"1951361732","Number of Group Practice members":4,"First Name":"KRISTEN","Last Name":"KOSIR","Credential":"OD","Primary specialty":"OPTOMETRY"},{"Organization legal name":"3-D OPTICAL","Group Practice PAC ID":"1951361732","Number of Group Practice members":4,"First Name":"NIROPA","Last Name":"PRASAD","Credential":"","Primary specialty":"OPTOMETRY"},{"Organization legal name":"3-D OPTICAL","Group Practice PAC ID":"1951361732","Number of Group Practice members":4,"First Name":"ROBERT","Last Name":"MCCAMY","Credential":"OD","Primary specialty":"OPTOMETRY"},{"Organization legal name":"3-D OPTICAL","Group Practice PAC ID":"1951361732","Number of Group Practice members":4,"First Name":"DARREL","Last Name":"CRISSLER","Credential":"","Primary specialty":"OPTOMETRY"},{"Organization legal name":"5MD CONVENIENT CARE LLC","Group Practice PAC ID":"2567643026","Number of Group Practice members":4,"First Name":"JULIE","Last Name":"HATCH","Credential":"","Primary specialty":"NURSE PRACTITIONER"}]
如果我登录到控制台,它看起来像有效的html
<li>23RD FAMILY MED LLC</li><li>23RD FAMILY MED LLC</li><li>23RD FAMILY MED LLC</li><li>23RD FAMILY MED LLC</li><li>23RD FAMILY MED LLC</li><li>3-D OPTICAL</li><li>3-D OPTICAL</li><li>3-D OPTICAL</li><li>3-D OPTICAL</li><li>5MD CONVENIENT CARE LLC</li>
如果我在浏览器中查看页面源代码(谢谢Tomasz Jakub Rup),那就不那么有效......
<body>
<h1>Group Practices</h1>
<p>Welcome to Group Practices</p>
<ul>
<li>23RD FAMILY MED LLC</li><li>23RD FAMILY MED LLC</li><li>23RD FAMILY MED LLC</li><li>23RD FAMILY MED LLC</li><li>23RD FAMILY MED LLC</li><li>3-D OPTICAL</li><li>3-D OPTICAL</li><li>3-D OPTICAL</li><li>3-D OPTICAL</li><li>5MD CONVENIENT CARE LLC</li>
</ul>
</body>
任何人都可以确定我做错了什么吗?看起来好像我的浏览器将其解释为文本字符串,而不是标记。
答案 0 :(得分:2)
将<%= html %>
替换为<%- html %>
ejs不那么流行的惯例:
<%=
生成转义值
<%-
生成未转义的值
lodash.template有一个反向约定(更受欢迎):
<%-
生成转义值
<%=
生成未转义的值