The title says it all. I saw there is a couple of template engines for node.js I am little confused which one I should learn or use. Share your experience or suggestion.
Express uses jade (renamed to pug) so Iāve seen it most often, but Iām not sure there is a ābestā. Itās sort of like CSS where there are many option (Sass, SCSS, compass, etc.) and they offer different features but itās up to you and your preferences. You could use regular olā HTML if you wanted to.
oh i though pug and jade are different .thanks for the response
I didānt get this part
I mean you could use html5 (plain html; not a view engine) with node if you really wanted to.
It might be a noob question but is there any way to inject data in regular HTML without using any template engine
No. Anything that injects code into HTML would be a templating engine by definition.
Yes, you can serve html from within pure javascript but the syntax is horrible. If I remember right, you have to wrap the html tags in quotes. Thatās what makes templating engines so attractive. Personally I like handlebars, because it is so simple - just write an ordinary html file and put your javascript and variables betweeen {{ }}.
I loved EJS as it was really simple to learn ā¦ You can use express with EJS or jade or even handle bars!! EJS was so similar to html so I found it easy . Python users would find jade easy I guess!
Love ejs, hate pug (jade) - white space sensitivity is hell.
Yes you can send data to the fronend by using just HTML and JavaScript without a templating language but its hard and I only recommend it if ur 1337 like me and can code it.
I think that it depends on what want to do with your content.
While using EJS I could manipulate it much better, because you are actually use all Javascript methods (well I think pretty much all of them).
Handlebars is really simple and honestly more readable than EJS in my opinion, but you cannot write complex logic without making helpers.
Never used jade(pug) before though so canāt commend on that.
I have learnt React recently, having had a quick look at many alternatives. React uses āJSXā, which is, in a way, the reverse of a templating engine. It allows you to put (almost) any XML/XHTML straight into your JavaScript.
I think this works well. It is easy to put code inside the markup, and markup inside the code. This gives you full flexibility as to which bits you generate statically and which bits dynamically.
Iām afraid I cannot really say whether other mechanisms are better or worse. It may depend on your preference, as well as the characteristics of the application.
If you are comparing, I suggest you take a look at Vue, as well as the others.
I initially loved EJS and hated React, but React is growing on me, and some of my more complicated EJS was borderline unreadable. I havenāt used Pug/Jade, Handlebars etc
With EJS I found myself mixing lots of logic with rendered output. With React, while you do mix JS and HTML in the same file, they are kept seperate enough that itās easier to reason about.
Handlebars for me. It was in the first video about node I watched so I probably like it for that reason.
I didnāt like the complexity of many of the template engines I found, and I wanted to write in simple HTML with options and basic conditionals, so I made my own at https://www.npmjs.com/package/squirrelly.
You could make a basic one yourself using this tutorial, or you could use mine
I am a beginner, I feel that ejs is easier for me to learn.
Would like to know what are the differences in features/functionality between ejs and pug.
I love pug. It makes everything look so pretty.
Iāve been using ejs for my express / (really basic) react projects and itās simple enough but something like https://github.com/reactjs/express-react-views gives more features, I might switch over
Pug is, I agree, harder to learn because it is not as similar to HTML as EJS is.
They serve pretty much the same purpose and achieve the same things. The basic features of both are adding scripts into your code and includes which help you organize. Pug has additional features which help you write HTML more efficiently such as mixins. Personally, I found EJS to be simpler to learn, but once I learned Pug, I like it a lot more as it has better functionality for me. The syntax of EJS can get cluttered very quick with all the percent signs, while pug is very readable, more pretty, and much faster to write than ordinary HTML.
Compare and contrast these two pieces of code which achieve about the same thing:
// conditional example
unless user.isAnonymous
p You're logged in as #{user.name}
// iteration example
ul
each val in [1, 2, 3, 4, 5]
li= val
vs.
// conditional example
<% if (!user.isAnonymous) { %>
<p>You're logged in as <%= user.name %></p>
<% } %>
// iteration example
<ul>
<% numberArray.forEach(function(val){ %>
<%= val %>
<% }); %>
</ul>
I find the pug much more attractive and easier to write.
Thanks for the comparison