如何转义 ejs 模板字符串中的 ejs 标签?

Gar*_*Dan 2 javascript ejs

我们如何在模板中转义 ejs 标签本身?

在 sails 中,我想将以下代码放入 layout.ejs 中

<!--STYLES-->
<% if (typeof head_css != 'undefined') { %>
  <%= head_css %>
<% } else { %>
  <% if (typeof head_css_before != 'undefined') { %>
    <%= head_css_before %>
  <% } %>
    <link rel="stylesheet" href="/styles/importer.css">
  <% if (typeof head_css_after != 'undefined') { %>
    <%= head_css_after %>
  <% } %>
<% } %>
<!--STYLES END-->
Run Code Online (Sandbox Code Playgroud)

<!--STYLES-->但默认情况下,和中的所有内容<!--STYLES END-->都会被此模板替换:

<link rel="stylesheet" href="%s">
Run Code Online (Sandbox Code Playgroud)

如在sails-linker.js.

因此,我不想放置模板,而是<link rel="stylesheet" href="%s">放置一些更复杂的东西,而不是渲染时的结果变成:

<!--STYLES-->
<% if (typeof head_css != 'undefined') { %>
  <%= head_css %>
<% } else { %>
  <% if (typeof head_css_before != 'undefined') { %>
    <%= head_css_before %>
  <% } %>
    <link rel="stylesheet" href="/styles/importer.css">
  <% if (typeof head_css_after != 'undefined') { %>
    <%= head_css_after %>
  <% } %>
<% } %>
<!--STYLES END-->
Run Code Online (Sandbox Code Playgroud)

但问题是当我尝试类似的事情时

<%= special_code_here %><link rel="stylesheet" href="%s">
Run Code Online (Sandbox Code Playgroud)

这是自动渲染的。我需要转义<%ejs 模板内部。

我们应该怎么做?

ill*_*crx 5

<%%转义 EJS 标签,并会给你<%

这是文档: EJS

如果我正确理解你的问题,这应该会有所帮助。