Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

javascript - 如何使用underscore.js作为模板引擎?(How to use underscore.js as a template engine?)

I'm trying to learn about new usages of javascript as a serverside language and as a functional language.

(我正在尝试了解javascript作为服务器端语言和函数式语言的新用法。)

Few days ago I heard about node.js and express framework.

(几天前我听说过node.js和表达框架。)

Then I saw about underscore.js as a set of utility functions.

(然后我看到underscore.js作为一组实用函数。)

I saw this question on stackoverflow .

(我在stackoverflow上看到了这个问题 。)

It says we can use underscore.js as a template engine.

(它说我们可以使用underscore.js作为模板引擎。)

anybody know good tutorials about how to use underscore.js for templating, especially for biginners who have less experience with advanced javascript.

(任何人都知道有关如何使用underscore.js进行模板化的好教程,特别是对于那些对高级javascript经验较少的biginners。)

Thanks

(谢谢)

  ask by knodumi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Everything you need to know about underscore template is here .

(您需要了解的关于下划线模板的所有内容都在这里 。)

Only 3 things to keep in mind:

(只需记住3件事:)

  1. <% %> - to execute some code

    (<% %> - 执行一些代码)

  2. <%= %> - to print some value in template

    (<%= %> - 在模板中打印一些值)

  3. <%- %> - to print some values HTML escaped

    (<%- %> - 打印一些HTML转义的值)

That's all about it.

(这就是它的全部。)

Simple example:

(简单的例子:)

var tpl = _.template("<h1>Some text: <%= foo %></h1>");

then tpl({foo: "blahblah"}) would be rendered to the string <h1>Some text: blahblah</h1>

(那么tpl({foo: "blahblah"})将呈现给字符串<h1>Some text: blahblah</h1>)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...