如何在 JS 字符串中使用 EJS 标签

<%
   let myString = "this is a string %> with special characters";
   let myotherString = "this is a string <% with special characters";
   // Do something
%>

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

编译 ejs 时无效或意外的标记

我应该怎么做?

limian6666 回答:如何在 JS 字符串中使用 EJS 标签

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

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

或者使用模板文字来让它不那么难看:

let startTag = '<' + '%';

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

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

本文链接:https://www.f2er.com/38726.html

大家都在问