如何使用 underscore.js 作为模板引擎? [英] How to use underscore.js as a template engine?
问题描述
我正在尝试了解 javascript 作为服务器端语言和函数式语言的新用法.几天前我听说了 node.js 和 express 框架.然后我看到 underscore.js 是一组实用函数.我在stackoverflow上看到了这个问题.它说我们可以使用 underscore.js 作为模板引擎.任何人都知道关于如何使用 underscore.js 进行模板制作的好教程,尤其是对于高级 javascript 经验较少的大人物.谢谢
I'm trying to learn about new usages of javascript as a serverside language and as a functional language. Few days ago I heard about node.js and express framework. Then I saw about underscore.js as a set of utility functions. I saw this question on stackoverflow . It says we can use underscore.js as a template engine. anybody know good tutorials about how to use underscore.js for templating, especially for biginners who have less experience with advanced javascript. Thanks
推荐答案
关于下划线模板你需要知道的一切都在这里.只需要记住 3 件事:
Everything you need to know about underscore template is here. Only 3 things to keep in mind:
<% %>
- 执行一些代码<%= %>
- 在模板中打印一些值<%- %>
- 打印一些 HTML 转义值
<% %>
- to execute some code<%= %>
- to print some value in template<%- %>
- to print some values HTML escaped
仅此而已.
简单例子:
var tpl = _.template("<h1>Some text: <%= foo %></h1>");
然后 tpl({foo: "blahblah"})
将被渲染为字符串 <h1>Some text: blahblah</h1>
then tpl({foo: "blahblah"})
would be rendered to the string <h1>Some text: blahblah</h1>
这篇关于如何使用 underscore.js 作为模板引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!