使用 ejs 将 Html 数据转换为文本
convert Html data into text using ejs
我正在从数据库中获取数据,在该数据中我发现了一些字符串形式的 HTML 标签,但是当我将数据显示到前端时,我看到了 HTML 标签也是。我想使用 ejs 将 HTML 标签数据转换为实际值。
我的密码是-
获取数据 -
app.get("/blogs/:id", (req, res) => {
const requestParams = _.lowerCase(req.params.id);
blogData.forEach(function (post) {
const storedTitel = _.lowerCase(post.heading);
if (storedTitel === requestParams) {
res.render("blogsfullDetails", {
date: post.date,
heading: post.heading,
subheading: post.subheading,
discription: post.discription,
discription2: post.discription2,
image: post.image,
image2: post.image2,
});
}
});
});
ejs文件-
<div class="blogTitle">
<p class="date text-center"><%= date %></p>
<p class="blog-heading text-center"><%= heading %></p>
<p class="subText text-center"><%= subheading %></p>
<div class="socialMedia">
<a href="" target="_blank" ><i class="fa fa-linkedin fa-2x" style="color: #0e76a8 ;" aria-hidden="true"></i></a>
<a href="" target="_blank" ><i class="fa fa-instagram fa-2x" style="color: #fb3958;"></i></a>
<a href="" target="_blank" ><i class="fa fa-facebook-square fa-2x" style="color: #4267B2;"></i></a>
</div>
</div>
<div class="blog-main-image" style="background-image: url('<%= image2 %>')"></div>
<div class="blogText">
<p><%= discription %></p>
<div class="blog-main-image" style="background-image: url('<%= image %>')"></div>
<p><%= discription2 %></p>
</div>
前端图片 -
我知道如何在 React 中使用 dangerouslySetInnerHTML 转换 Html 文本中的数据,但不知道如何在 ejs 中转换,请帮助
使用 <%- html_variable ->
语法而不是 <%= variable >
,其中需要呈现原始 HTML 代码。
更新:
为了安全起见,我建议在直接使用之前先解析 html 。
查看此步骤:What is "Escaped" & "Unescaped" output 并阅读有关跨站点脚本 (XSS) 攻击的信息。
我正在从数据库中获取数据,在该数据中我发现了一些字符串形式的 HTML 标签,但是当我将数据显示到前端时,我看到了 HTML 标签也是。我想使用 ejs 将 HTML 标签数据转换为实际值。
我的密码是-
获取数据 -
app.get("/blogs/:id", (req, res) => {
const requestParams = _.lowerCase(req.params.id);
blogData.forEach(function (post) {
const storedTitel = _.lowerCase(post.heading);
if (storedTitel === requestParams) {
res.render("blogsfullDetails", {
date: post.date,
heading: post.heading,
subheading: post.subheading,
discription: post.discription,
discription2: post.discription2,
image: post.image,
image2: post.image2,
});
}
});
});
ejs文件-
<div class="blogTitle">
<p class="date text-center"><%= date %></p>
<p class="blog-heading text-center"><%= heading %></p>
<p class="subText text-center"><%= subheading %></p>
<div class="socialMedia">
<a href="" target="_blank" ><i class="fa fa-linkedin fa-2x" style="color: #0e76a8 ;" aria-hidden="true"></i></a>
<a href="" target="_blank" ><i class="fa fa-instagram fa-2x" style="color: #fb3958;"></i></a>
<a href="" target="_blank" ><i class="fa fa-facebook-square fa-2x" style="color: #4267B2;"></i></a>
</div>
</div>
<div class="blog-main-image" style="background-image: url('<%= image2 %>')"></div>
<div class="blogText">
<p><%= discription %></p>
<div class="blog-main-image" style="background-image: url('<%= image %>')"></div>
<p><%= discription2 %></p>
</div>
前端图片 -
我知道如何在 React 中使用 dangerouslySetInnerHTML 转换 Html 文本中的数据,但不知道如何在 ejs 中转换,请帮助
使用 <%- html_variable ->
语法而不是 <%= variable >
,其中需要呈现原始 HTML 代码。
更新: 为了安全起见,我建议在直接使用之前先解析 html 。 查看此步骤:What is "Escaped" & "Unescaped" output 并阅读有关跨站点脚本 (XSS) 攻击的信息。