What is EJS and why do I need it?

What is EJS and why do I need it?

EJS is a templating engine for for JavaScript. So, font-end stuff.

It seems pretty laissez-faire in that it doesn't tell you how to organise things. Ultimately, EJS is a tool to generate HTML markup with plain Javascript.

The general idea

Send your content to the browser delivered as js, in whatever syntax your chosen js templating engine dictates, once loaded (probably on DOMContentLoaded?) at which point the js template engine will take over and generate the HTML client side. Seems a little backward, but then you get the advantage of less a complicated server side and end up pushing rendering to the client. This is quite similar to how IP networks function (ie the internet!) in that all the complexity is essentially pushed to the outer layers leaving the core to become simpler and therefore more efficient and scalable.

What are these js templating engines anyway?

  • Do they work as well server side and client side?
  • The general idea is to be working with the same language client and server side (isomorphic)
  • They all basically seem to achieve the same thing, with different semantics
  • The end result is always html...

Alex Gorbatchev wrote a useful comparison of Javascript Templating Engines in 2014. It's interesting to see therefore which are still being used as we approach 2018.

It integrates with the Express View Engine.

Other js template engines which integrate with Express are Pug and Moustache with Express favouring Jade/Pug by default. - Heads up: Jade has been renamed to pug..so watch out for that in documentation.

On first glance Moustache seems to be more substantial than any of the above engines with many languages implementing. However be aware that Moustache is "logic-less" meaning that it has no 'if/else' or 'loops'. This keeps it simple for a simple value / replace system but might not be what you're after.

The code is here

Collect recurring payments with Subscribie - Try Now