如何在node.js EJS视图中转义HTML? [英] How to escape HTML in node.js EJS view?

查看:136
本文介绍了如何在node.js EJS视图中转义HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在bloglist [i] .Text字段中转义html。如何用EJS做到这一点?

I want to escape the html in bloglist[i].Text field. How to do that with EJS?

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1><%= title %></h1>
    <p>Welcome to <%= title %></p>
    <% for(var i=0; i < bloglist.length; i++) { %>
       <h3> <%= bloglist[i].Title %></h3>
       <div>
          <%= bloglist[i].Text %>
       </div>
    <% } %>
  </body>
</html>


推荐答案

p>

You are escaping the value correctly by using:

<%= bloglist[i].Text %>

如果您想允许呈现HTML,那么您需要一个未转义值。为此,请使用以下内容:

If you want to allow HTML to be rendered, then you want an "unescaped" value. To do that use the following:

<%- bloglist[i].Text %>

我所做的只是用破折号( - )替换等号(=)。

All I did was replace the equal (=) with a dash (-).

参考: https://github.com/visionmedia/ejs /tree/0.8.3#features

这篇关于如何在node.js EJS视图中转义HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆