Jade和EJS for Node.js模板的优缺点是什么? [英] What are the pros and cons of both Jade and EJS for Node.js templating?

查看:109
本文介绍了Jade和EJS for Node.js模板的优缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jade与EJS的区别是什么,每种目的是什么?

Jade versus EJS, what are the pros and cons of each and what purposes are each designed for?

是否存在其他任何与表达兼容的模板引擎都很好,为什么?

Are there any other express-compatible template engines that are good and why?

推荐答案

我以前使用过Jade。关于Jade的好处是语法较短,这意味着您可以更快地键入。 Jade中的 block 非常强大,在处理复杂的HTML代码时可以为我提供很多帮助。

I used Jade before. The nice thing about Jade is that you have a shorter syntax which means you can type faster. The block in Jade is pretty powerful which can help me a lot when dealing with complex HTML code.

另一方面,很难在Jade中做一些简单的事情,例如基于简单的if条件将类添加到DIV中。我需要输入以下内容

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

- if (isAdmin)
  div.admin.user
- else
  div.user

Jade也不区分标记和变量使代码非常混乱(至少对我来说如此)

Jade also don't differentiate between the tags and the variables which make the code very confusing (at least for me)

a(href='/user/' + user.id)= user.name

翡翠对设计师也不友好。我的设计师朋友经常给我HTML和CSS(他们最近改用LESS,但仍想使用HTML),因此,如果我使用Jade,则需要将HTML转换为Jade。同样在Jade中,我们需要使用缩进,因此,如果您的HTML结构变得复杂,您的代码将看起来很恐怖(尤其是表格)。有时候,我什至不知道自己处于什么水平

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

table
  thead
    tr
      td
        a
          img
    tr
      td
  tbody
    tr
      td

最近,我改用EJS,到目前为止,我对此感到满意。它非常接近纯HTML,并且使用与我使用的前端模板引擎(Underscore模板)相同的语法。我必须说,使用EJS可使一切变得容易。从设计师朋友那里收到HTML模板时,我不必做所有转换。我要做的就是用ExpressJS传递的变量替换动态部件。在EJS中解决了让我使用Jade时使我疯狂的东西

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

<div class="<%= isAdmin? 'admin': '' %> user"></div>

我可以知道EJS是什么东西

And I can know what is what with EJS

<a href="/user/<%= user.id %>"><%= user.name %></a>

如果您错过了Jade的简短语法(像我一样),可以将Zen-Coding和EJS结合使用可以帮助您总体上加快进度。关于性能,我看不出有什么区别

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

但是,EJS不如Jade强大,默认情况下它没有阻止功能(此人实现了阻止功能用于EJS https://github.com/RandomEtc/ejs-locals

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)

因此,完全取决于您选择让自己感到舒适的东西。但是,如果您打算像我一样使用其他模板引擎作为前端,最好是对双方都使用相同的东西

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

2013年12月16日更新:
最近,我从EJS切换到Swig(其概念与Python世界中的Jinja2类似)。主要原因是即使在 ejs-locals 的帮助下,EJS中也没有阻塞。 Swig还使用纯HTML模板和模板引擎应具有的许多很酷的功能,例如EJS没有的过滤器和标签

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

这篇关于Jade和EJS for Node.js模板的优缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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