如何使用 underscore.js 作为模板引擎? [英] How to use underscore.js as a template engine?

查看:24
本文介绍了如何使用 underscore.js 作为模板引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 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:

  1. <% %> - 执行一些代码
  2. <%= %> - 在模板中打印一些值
  3. <%- %> - 打印一些 HTML 转义值
  1. <% %> - to execute some code
  2. <%= %> - to print some value in template
  3. <%- %> - 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆