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

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

问题描述

我试图了解JavaScript的新用法作为服务器端语言和功能语言。几天前我听说过node.js和表达框架。然后我将underscore.js看作一组实用函数。我在 stackoverflow
上看到了这个问题。它说我们可以使用underscore.js作为模板引擎。任何人都知道关于如何使用underscore.js进行模板化的好教程,特别是对于那些对高级javascript没有经验的biginners。谢谢 解决方案

你需要知道的关于下划线模板的一切是这里。请注意以下三点:


  1. <%%> - 执行一些代码

  2. <%=%> - 在模板中打印一些值










  3. $



    简单的例子:

      var tpl = _.template(< h1>一些文本:<%= foo%>< / h1>); 

    然后 tpl({foo:blahblah})将呈现给字符串< h1>某些文本:blahblah< / h1>


    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

    解决方案

    Everything you need to know about underscore template is here. Only 3 things to keep in mind:

    1. <% %> - to execute some code
    2. <%= %> - to print some value in template
    3. <%- %> - to print some values HTML escaped

    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>

    这篇关于如何使用underscore.js作为模板引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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