简体   繁体   English

如何在 JS 字符串中使用 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
%>

I need to use these special characters in my JS string <% and/or %>.我需要在我的 JS 字符串 <% 和/或 %> 中使用这些特殊字符。 But EJS template would return errors: Could not find matching close tag for "<%" or SyntaxError: Invalid or unexpected token while compiling ejs for %>但是 EJS 模板会返回错误:找不到匹配的“<%”的结束标记或语法错误:在为 %> 编译 ejs 时无效或意外的标记

How should I go about this?我应该怎么做?

You could simply concatenate two strings together:您可以简单地将两个字符串连接在一起:

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

or with template literals to make it a bit less ugly:或使用模板文字使其不那么难看:

let startTag = '<' + '%';

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

For the record, their documentation lists options to change the starting/ending tags, which they call delimiters, in case you prefer eg <!作为记录,他们的文档列出了更改开始/结束标签的选项,他们称之为分隔符,以防您喜欢例如<! and !> .!> Their documentation also mentions that <%% should print <% but I personally don't know if this works within a <% code %> block.他们的文档还提到<%%应该打印<%但我个人不知道这是否适用于<% code %>块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM