惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
S
Securelist
P
Proofpoint News Feed
H
Help Net Security
S
Schneier on Security
T
Tenable Blog
C
Cisco Blogs
S
Security @ Cisco Blogs
博客园 - 司徒正美
博客园 - 叶小钗
Cisco Talos Blog
Cisco Talos Blog
Google DeepMind News
Google DeepMind News
C
Cybersecurity and Infrastructure Security Agency CISA
Google Online Security Blog
Google Online Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News: Ask HN
Hacker News: Ask HN
NISL@THU
NISL@THU
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
W
WeLiveSecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
腾讯CDC
WordPress大学
WordPress大学
Simon Willison's Weblog
Simon Willison's Weblog
Vercel News
Vercel News
小众软件
小众软件
N
Netflix TechBlog - Medium
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
雷峰网
雷峰网
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
博客园 - 聂微东
F
Full Disclosure
量子位
Scott Helme
Scott Helme
宝玉的分享
宝玉的分享
A
About on SuperTechFans
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Schneier on Security
Schneier on Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
Kaspersky official blog
AI
AI
SecWiki News
SecWiki News
Webroot Blog
Webroot Blog
Martin Fowler
Martin Fowler

博客园 - Adam.Zhao

简单示例理解神闭包 我使用的开源组件汇总,以备学习使用 了不起的Node.js--之五 TCP连接 Windows7下Java运行时环境搭建 了不起的Node.js--之四 了不起的Node.js--之三 了不起的Node.js--之二 了不起的Node.js--之一 请大家有需要购物的到我的网站上吧。 AngularJS应用骨架 最近在研究google的angularjs 好久没有来写博客了 c#二进制文件数据转换base64字符串文本代码 ActionFilterAttribute做切面编程的Url的格式化例子 学习content-type 网站性能优化之服务端(一) 艺龙旅行网招聘了 关于项目管理的几点建议 HP CQ35 Windows7声卡驱动安装不上问题
ejs 模板使用方法
Adam.Zhao · 2014-08-28 · via 博客园 - Adam.Zhao

http://embeddedjs.com/

Embedded JS Templates

Embedded JS(EJS) 来源于ERB模板,且与ERB有很多相似之处。它有着与ERB相同的Tag,且包含很多相同的功能。EJS的特别之处在于,你需要把模板存于单独文件中,并将文件名传递给EJS。它会加载该文件,并返回HTML。

// in template.ejs
Hello, <%= name %>

// in JS file
new EJS({ url: "template.ejs" }).render({ name: "Jack" });
// 返回: Hello, Jack

复制代码
注意,你可以加载文本模板:

new EJS({ text: "Hello, <%= name %>" }).render({ name: "Jack" });


复制代码
下面将介绍如何进行循环,以数组“People”为例,并在网站上链接到他们的个人页面:

// template.ejs
<ul>
<% for(var i = 0; i < people.length; i++) { %>
<li><%= link_to(people[i], "/profiles/" + people[i]) %></li>
<% } %>
</ul>

// in JS file
new EJS({ url: "template.ejs" }).render({ people: [ "Jack", "Fred" ] })

// Each rendered <li> will look like:
<li><a href="/profiles/Jack">Jack</a></li>

复制代码

这与Underscore 有些相似,但要注意“link_to”的使用。它是EJS定义的一个Helper,以便链接更容易使用。了解更多EJS,请关注EJS官方网站。