What are the pros and cons of both Jade and EJS for Node.js templating?
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?
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.
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 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
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
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>
And I can know what is what with 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
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
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
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.
However, Jade is rather clean and makes for some neat code in your views.
Pick whatever you feel more comfortable.
链接地址: http://www.djcxy.com/p/88900.html上一篇: Jade条件中的换行符