【问题标题】:How to use EJS tags in a JS string如何在 JS 字符串中使用 EJS 标签
【发布时间】:2021-07-25 20:13:21
【问题描述】:
<%
   let myString = "this is a string %> with special characters";
   let myOtherString = "this is a string <% with special characters";
   // Do something
%>

我需要在我的 JS 字符串 中使用这些特殊字符。但是 EJS 模板会返回错误:找不到匹配的关闭标记“

我该怎么办?

【问题讨论】:

    标签: javascript tags ejs


    【解决方案1】:

    您可以简单地将两个字符串连接在一起:

    let myString = "this is a string %" + "> with special characters";
    

    或使用模板文字使其不那么难看:

    let startTag = '<' + '%';
    
    let myString = `this is a string ${startTag} with special characters`;
    

    作为记录,their documentation 列出了更改开始/结束标签的选项,他们称之为分隔符,以防您更喜欢例如&lt;!!&gt;。他们的文档还提到 &lt;%% 应该打印 &lt;% 但我个人不知道这是否适用于 &lt;% code %&gt; 块。

    【讨论】:

    • 谢谢!连接 2 个字符串对我有用。
    • @akl 然后不要忘记将此答案标记为您问题的解决方案
    猜你喜欢
    • 1970-01-01
    • 2016-04-23
    • 2012-10-18
    • 1970-01-01
    • 2021-06-28
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多