![](/img/trans.png)
[英]How to pass html tags in a string with node.js, express and ejs
[英]How to use EJS tags in a JS string
<%
let myString = "this is a string %> with special characters";
let myOtherString = "this is a string <% with special characters";
// Do something
%>
我需要在我的 JS 字符串 <% 和/或 %> 中使用這些特殊字符。 但是 EJS 模板會返回錯誤:找不到匹配的“<%”的結束標記或語法錯誤:在為 %> 編譯 ejs 時無效或意外的標記
我應該怎么做?
您可以簡單地將兩個字符串連接在一起:
let myString = "this is a string %" + "> with special characters";
或使用模板文字使其不那么難看:
let startTag = '<' + '%';
let myString = `this is a string ${startTag} with special characters`;
作為記錄,他們的文檔列出了更改開始/結束標簽的選項,他們稱之為分隔符,以防您喜歡例如<!
和!>
。 他們的文檔還提到<%%
應該打印<%
但我個人不知道這是否適用於<% code %>
塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.