[英]insert script tag with data using ejs (ejs-locals) on express.js
I'm using ejs-locals for express 3.x (https://github.com/RandomEtc/ejs-locals) 我正在使用ejs-locals for express 3.x(https://github.com/RandomEtc/ejs-locals)
How would I insert a script tag from a template with dynamic data? 如何从包含动态数据的模板插入脚本标记?
In my layout.ejs I have 在我的layout.ejs中我有
<%- block.scripts %>
In my page template login.ejs I want to replace it with some dynamic data: 在我的页面模板login.ejs中我想用一些动态数据替换它:
<% block('scripts', "<script> var app = window.app || {}; app.err = <%- JSON.stringify(err) %>, app.q = <%- JSON.stringify(q) %>; </script>") -%>
I get an error: 我收到一个错误:
500 SyntaxError: Unexpected token %
-- i'm assuming because I can't do <%= JSON.stringigy(err) %>
here. 500 SyntaxError: Unexpected token %
- 我假设因为我不能在这里做<%= JSON.stringigy(err) %>
。
I solved the problem by moving the script insertion to layout.ejs since it's always the same. 我通过将脚本插入移动到layout.ejs来解决问题,因为它始终是相同的。
<% include script %>
</body>
Otherwise, you have to terminate the string: 否则,您必须终止字符串:
//this works but is rather cumbersome to do on every page template.
<% block('script', '<script>' +
'app.req.err = '+ JSON.stringify(err) +";\n" +
'app.req.q = '+ JSON.stringify(q) +";\n" +
'</script>') %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.