簡體   English   中英

使用express.js上的ejs(ejs-locals)在數據中插入腳本標記

[英]insert script tag with data using ejs (ejs-locals) on express.js

我正在使用ejs-locals for express 3.x(https://github.com/RandomEtc/ejs-locals)

如何從包含動態數據的模板插入腳本標記?

在我的layout.ejs中我有

<%- block.scripts %>

在我的頁面模板login.ejs中我想用一些動態數據替換它:

<% block('scripts', "<script> var app = window.app || {}; app.err = <%- JSON.stringify(err) %>, app.q = <%- JSON.stringify(q) %>; </script>") -%>

我收到一個錯誤:

500 SyntaxError: Unexpected token % - 我假設因為我不能在這里做<%= JSON.stringigy(err) %>

我通過將腳本插入移動到layout.ejs來解決問題,因為它始終是相同的。

    <% include script %>
  </body>

否則,您必須終止字符串:

//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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM