Templating engine - ybouz2/project-tech GitHub Wiki
A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.
You can create parts of code that u can use on multiple pages, like your header or footer. And include this on every page. so you have to write the code just once. The templating engine will compile this to regular HTML and put everything together for you.
You can also use the templating engine to pass data trough from your database for example.
#Compare Templating engines
EJS simply stands for Embedded Javascript. It is a simple templating language/engine that lets its user generate HTML with plain javascript. EJS is mostly useful whenever you have to output HTML with a lot of javascript.
- EJS uses all the JS jargon and logic, so if you're proficient in JS, you can use EJS right away.
- Your html/text remains pretty much the same before and after rendering. EJS filters out and performs its functions on any occurrences of its own <%= %> tags in your template.
- Easy to learn. The syntax itself is easy to comprehend for anyone who is even somewhat familiar with JavaScript and CSS.
- According to some benchmark tests, EJS is way faster than Jade or Haml.
- EJS has a really smart error handling mechanism built right into it.
- EJS has no support for the
block
functionality which allows you to reuse pieces of templates across different files. - Difficult to read.
PUG is a Javascript library that was previously known as JADE. It is an easy-to-code template engine used to code HTML in a more readable fashion. One upside to PUG is that it equips developers to code reusable HTML documents by pulling data dynamically from the API.
- Clean Syntax
- Allows writing inline JavaScript
- Reuse code in other languages
- High performance on the server and client side
- Bad performance
- Unforgiving in case of indentation errors
- Cannot copy/paste examples from the internet
- Small Community
Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions.
- Clear separation of logic and markup
- Copy/Paste code from the internet
- Easy to use any template also as partials
- Good global helpers support
- Handlebars works only with HTML code
- Hard to read documentation
My choice ultimately goes to handlebars, the reason for this is that there is a lot to be found about this template engine and there is a large community for it. Another reason to use this is because it's easy to read and my teacher also prefers Handlebars.
sources: