linkify urls in templates-nodejs
linkify urls in templates-nodejs
认为我的查询从标题本身就很清楚了。如何在 ejs 模板中呈现时将 url 字符串与 href 属性链接起来?是否有任何帮助者可用于此任务?我想过将 url 与其 href 属性一起转换为字符串,然后将它们发送到模板,但我想它们会在渲染时被转义。
即使我使用 ejs 作为视图..如果任何其他引擎具有此链接功能,我想更改模板引擎。
更新:
假设这是渲染函数
return res.render("polo", {
title: "Posters",
has_next:false,
posters:posters,
offset:0
});
这是视图
<% for (var i=0;i<posters.length;i++) { %>
<div id="<%=posters[i]._id %>" class="newstitle">
<p>
<%=posters[i].body %>
</p>
</div>
<% }; %>
现在,如果海报[1].body="你好,这是一个很好的网站。 www.google.com" .同样,海报数组中的所有其他元素都是一些包含一些 url 的文本。当 posters[i].body 呈现时,我希望链接上面示例中的"google.com"等 URL。我不能使用 <%= url %>,因为我不知道 poster.body 中的网址到底在哪里。
如果你想输出到href属性而不转义,那么,你可以使用<a href="<%- yourvar %>">the link.</a>
,它不会转义。
(请注意,使用连字符而不是等号。
编辑
OP 询问如何仅在定义 url 时显示链接。
<% if (typeof yourvar != 'undefined' && yourvar !== null) { %>
<a href="<%- yourvar %>">the link</a>
<% } %>
相关文章:
- Require in nodejs
- ReadFile in Base64 Nodejs
- What is the reason for cloning the listeners array in nodejs
- Schema.pre in Mongoose modules (NodeJS)
- linkify urls in templates-nodejs
- NodeJS put array in MongoDB doc.validate 不是一个函数
- setTimeout in nodejs - for loops
- 无法在 nodeJS:TypeError 中对用户进行身份验证:无法使用“in”运算符在 fs 中搜索“用户名”
- setInterval in nodejs
- Understanding {{.0}} {{.1}} in stache templates
- setTimeout in nodejs
- removeListener in the Listener in nodejs
- Stream in NodeJS
- RabbitMQ no ack in nodejs
- NodeJS MSSQL WHERE IN准备的SQL语句
- res.send vs res.locals in nodejs
- cURL post request in Nodejs.
- Adding in Javascript (NodeJS)
- Github Post-Receive Hook in Nodejs
- DRY in Nodejs Express