Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a template engine? [duplicate]

Tags:

javascript

Possible Duplicate:
Javascript templating engines?

I keep on stumbling upon javascript templating engines and I have no idea what is their purpose. Could anyone explain me like I were five what are those? Can front-end developer make use of these?

like image 297
metrampaz Avatar asked Mar 03 '12 14:03

metrampaz


People also ask

What does a template engine do?

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.

Are template engines Good?

Using template engines for complex front end rendering is bad and not a good practice.

What is the most popular template engine?

Popular template engines Template engines are mostly used for server-side applications that are run on only one server and are not built as APIs. The popular ones include Ejs, Jade, Pug, Mustache, HandlebarsJS, Jinja2, and Blade.

Is templating engine necessary?

You actually dont need them, but they have a lot of features that makes your pages more dynamic..


1 Answers

JavaScript template engines are often used when writing thick JS-clients or as "normal" template engines when using server-side js like Node.js.

Instead of cluttering the code by generating HTML using string concatenating etc., you instead use a template and interpolate the variable in them.

For instance in the example bellow you pass in an array foo to the template engine which then interpolates (replaces the placeholder ${data}) with item i from the array foo:

//pseudo code <table>  <tr>    for each variable data in foo     <td>${data}</td>    end  <tr> </table> 

In frameworks like backbone.js you typically use a template to implement the view part of the MVC pattern.

There are many different types of template engines in JS. Some are direct ports of server-side frameworks like ERB, HAML etc. Others are developed specially to work with JavaScript i.e Pure

There is a debate of how much expressiveness the template engine should give you because putting logic in your templates is usually a bad thing. Because of this some template engines are specifically designed to not let you put logic in them i.e Mustache

like image 153
ebaxt Avatar answered Oct 02 '22 17:10

ebaxt