如何使用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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您需要了解的有关下划线模板的所有信息都位于此处。只需记住 3 件事:
<% %>
- 执行某些代码<%= %>
- 在模板中打印某些值<%- %>
- 打印一些 HTML 转义的值这就是全部。
简单的例子:
然后
tpl({foo: "blahblah"})
将被渲染为字符串Some text: blahblah
Everything you need to know about underscore template is here. Only 3 things to keep in mind:
<% %>
- to execute some code<%= %>
- to print some value in template<%- %>
- to print some values HTML escapedThat's all about it.
Simple example:
then
tpl({foo: "blahblah"})
would be rendered to the string<h1>Some text: blahblah</h1>
在最简单的形式中,您可以这样使用它:
如果您要多次使用模板,您将需要对其进行编译,以便速度更快:
我个人更喜欢 Mustache 样式语法。您可以调整模板标记标记以使用双花括号:
In it's simplest form you would use it like:
If you're going to be using a template a few times you'll want to compile it so it's faster:
I personally prefer the Mustache style syntax. You can adjust the template token markers to use double curly braces:
模板的文档是部分的,我看了源代码。
_.template 函数有 3 个参数:
如果没有给出数据(或 null),则将使用 render 函数回来了。它有 1 个参数:
设置中有 3 个正则表达式模式和 1 个静态参数:
evaluate 部分中的代码将被简单评估。您可以使用 __p+="mystring" 命令将此部分中的字符串添加到评估的模板中,但不建议这样做(不是模板界面的一部分),请使用插值部分来代替。这种类型的部分用于向模板添加 if 或 for 等块。
插值部分中的代码结果将添加到评估的模板中。如果返回 null,则将添加空字符串。
escape 部分在给定代码的返回值上使用 _.escape 转义 html。因此它与 interpolate 部分中的 _.escape(code) 类似,但它使用 \ 空白字符进行转义,例如 \ n 在将代码传递给 _.escape 之前。我不知道为什么这么重要,它在代码中,但它与 interpolate 和 _.escape 配合得很好 - 它不会逃脱空白人物——也是。
默认情况下,data 参数由 with(data){...} 语句传递,但这种评估比使用命名变量的评估慢得多。因此,使用 variable 参数命名 data 是件好事...
例如:
results
您可以在此处找到更多如何使用模板并覆盖默认设置的示例:
http://underscorejs.org/#template
通过模板加载,你有很多选择,但最后你总是必须将模板转换为字符串。您可以像上面的示例一样将其作为普通字符串提供,也可以从脚本标记加载它,并使用 。 jquery 的 html() 函数,或者您可以使用 tpl 插件< require.js 的 /a>。
使用 laconic 而不是模板构建 dom 树的另一种选择。
The documentation for templating is partial, I watched the source.
The _.template function has 3 arguments:
If no data (or null) given, than a render function will be returned. It has 1 argument:
There are 3 regex patterns and 1 static parameter in the settings:
The code in an evaluate section will be simply evaluated. You can add string from this section with the __p+="mystring" command to the evaluated template, but this is not recommended (not part of the templating interface), use the interpolate section instead of that. This type of section is for adding blocks like if or for to the template.
The result of the code in the interpolate section will added to the evaluated template. If null given back, then empty string will added.
The escape section escapes html with _.escape on the return value of the given code. So its similar than an _.escape(code) in an interpolate section, but it escapes with \ the whitespace characters like \n before it passes the code to the _.escape. I don't know why is that important, it's in the code, but it works well with the interpolate and _.escape - which doesn't escape the white-space characters - too.
By default the data parameter is passed by a with(data){...} statement, but this kind of evaluating is much slower than the evaluating with named variable. So naming the data with the variable parameter is something good...
For example:
results
You can find here more examples how to use the template and override the default settings:
http://underscorejs.org/#template
By template loading you have many options, but at the end you always have to convert the template into string. You can give it as normal string like the example above, or you can load it from a script tag, and use the .html() function of jquery, or you can load it from a separate file with the tpl plugin of require.js.
Another option to build the dom tree with laconic instead of templating.
我给出一个非常简单的例子
1)
结果是
2) 这是一个模板
这是 html
这是包含 json 对象并将模板放入 html 的 javascript 代码
I am giving a very simple example
1)
The result would be
2) This is a template
This is html
This is the javascript code which contains json object and putting template into html
有了快递就这么简单。您所需要的只是在节点上使用 consolidate 模块,因此您需要安装它:
然后您应该通过以下方式将默认引擎更改为 html 模板:
为 html 扩展注册下划线模板引擎:
它是完毕 !
现在加载例如名为“index.html”的模板:
我希望这对您有帮助!
with express it's so easy. all what you need is to use the consolidate module on node so you need to install it :
then you should change the default engine to html template by this:
register the underscore template engine for the html extension:
it's done !
Now for load for example an template called 'index.html':
I hope this helped you!
我想分享一项更重要的发现。
使用 <%= 变量 =>会导致跨站点脚本漏洞。所以使用<%-变量->比较安全反而。
我们必须将 <%= 替换为 <%- 以防止跨站点脚本攻击。不确定,这是否会对性能产生影响
I wanted to share one more important finding.
use of <%= variable => would result in cross-site scripting vulnerability. So its more safe to use <%- variable -> instead.
We had to replace <%= with <%- to prevent cross-site scripting attacks. Not sure, whether this will it have any impact on the performance
洛达什也是一样
首先写一个脚本如下:
现在写一些简单的JS如下:
其中popoup是你要生成表格的div
Lodash is also the same
First write a script as follows:
Now write some simple JS as follows:
Where popoup is a div where you want to generate the table