Jade 和 EJS 对 Node.js 模板的优缺点是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16513168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the pros and cons of both Jade and EJS for Node.js templating?
提问by HaoQi Li
Jade versus EJS, what are the pros and cons of each and what purposes are each designed for?
Jade 与 EJS,各自的优缺点是什么,各自的设计目的是什么?
Are there any other express-compatible template enginesthat are good and why?
是否有其他与 express 兼容的模板引擎是好的,为什么?
回答by Tan Nguyen
I used Jade before. The nice thing about Jade is that you have a shorter syntax which means you can type faster. The blockin Jade is pretty powerful which can help me a lot when dealing with complex HTML code.
我以前用过玉。Jade 的好处是你的语法更短,这意味着你可以更快地打字。该block翡翠是非常强大的,它可以帮助我很多复杂的HTML代码的时候。
On the other hand, it is hard to do some simple stuff in Jade, thing like adding classes into a DIV based on a simple if condition. I need to put something like this
另一方面,在 Jade 中很难做一些简单的事情,比如根据简单的 if 条件将类添加到 DIV 中。我需要放这样的东西
- if (isAdmin)
div.admin.user
- else
div.user
Jade also don't differentiate between the tags and the variables which make the code very confusing (at least for me)
Jade 也不区分标签和变量,这使代码非常混乱(至少对我而言)
a(href='/user/' + user.id)= user.name
Jade is also not designer-friendly. My designer friends often give me HTML and CSS (They switched to LESS recently but still want to use HTML), and for that reason if I use Jade I need to convert HTML to Jade. Also in Jade, we need to use indentations, so if your HTML structure gets complicated, your code will look horrible (especially tables). Sometimes, I don't even know what level I am at
翡翠也不是设计师友好的。我的设计师朋友经常给我 HTML 和 CSS(他们最近切换到 LESS 但仍然想使用 HTML),因此如果我使用 Jade,我需要将 HTML 转换为 Jade。同样在 Jade 中,我们需要使用缩进,因此如果您的 HTML 结构变得复杂,您的代码将看起来很糟糕(尤其是表格)。有时,我什至不知道自己处于什么水平
table
thead
tr
td
a
img
tr
td
tbody
tr
td
Recently, I made a switch to EJS and I am happy with it so far. It is very close to pure HTML and use the same syntax as that of the frontend template engine I am using (Underscore template). I must say that everything is easier with EJS. I don't have to do all the conversion when receiving HTML templates from my designer friend. All I have to do is to replace the dynamic parts with variables passed from ExpressJS. Stuff that make me crazy when using Jade are solved in EJS
最近,我改用了 EJS,到目前为止我很满意。它非常接近纯 HTML,并使用与我正在使用的前端模板引擎(Underscore 模板)相同的语法。我必须说 EJS 使一切变得更容易。从我的设计师朋友那里收到 HTML 模板时,我不必进行所有的转换。我所要做的就是用从 ExpressJS 传递的变量替换动态部分。使用 Jade 时让我发疯的东西在 EJS 中解决了
<div class="<%= isAdmin? 'admin': '' %> user"></div>
And I can know what is what with EJS
我可以知道 EJS 是什么
<a href="/user/<%= user.id %>"><%= user.name %></a>
If you miss the short syntax of Jade (like me) you can combine Zen-Coding and EJS which can help you speed up the progress in general. About performance, I don't see any differences
如果您错过了 Jade 的简短语法(像我一样),您可以结合 Zen-Coding 和 EJS,这可以帮助您加快总体进度。关于性能,我没有看到任何差异
However, EJS is not as powerful as Jade, it doesn't have blocks by default (this guy implemented a block feature for EJS https://github.com/RandomEtc/ejs-locals)
但是,EJS 没有 Jade 强大,它默认没有块(这家伙为 EJS 实现了块功能https://github.com/RandomEtc/ejs-locals)
So, it is totally depend on you to pick whatever makes you comfortable. But if you are going to use another template engine for the frontend like me, it's better if you use the same thing for both sides
所以,选择什么让你舒服完全取决于你。但是如果你要像我一样为前端使用另一个模板引擎,最好双方都使用相同的东西
Update 16 December 2013:
Recently, I have switched from EJS to Swig (which has similar concept as that of Jinja2 in Python world). The main reason is the lack of block in EJS even with the help of ejs-locals. Swig is also using plain HTML for templates and a lot of cool features that a template engine should have for example filters and tags which EJS doesn't have
2013 年 12 月 16 日更新:最近,我已从 EJS 切换到 Swig(其概念与 Python 世界中的 Jinja2 类似)。主要原因是即使在ejs-locals. Swig 还在模板中使用纯 HTML 以及模板引擎应该具有的许多很酷的功能,例如 EJS 没有的过滤器和标签
回答by Hector Correa
I wouldn't say that one is better than the other. They are different, that's for sure, but "better" is quite relative term.
我不会说一个比另一个更好。它们是不同的,这是肯定的,但“更好”是一个相对的术语。
I prefer EJS because I think HTML is not too bad, plus it allows me to work with others without them having to learn Jade.
我更喜欢 EJS,因为我认为 HTML 还不错,而且它使我可以与其他人一起工作,而无需他们学习 Jade。
However, Jade is rather clean and makes for some neat code in your views.
然而,Jade 相当干净,可以在您的视图中生成一些简洁的代码。
Pick whatever you feel more comfortable.
选择你觉得更舒服的任何东西。

