Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we get away with replacing existing JS templating solutions with ES6 templates?

One very attractive feature of ES6 is its built in template strings. At this point in time, since transpiling to ES5 is a must for cross browser compatibility, I am curious what the performance differences are between transpiled ES6 templates and existing solutions such as Mustache, Handlebars, Jade etc. Obviously if you need advanced features from a templating language, ES6 templates may not fulfill all of your needs, but if you are performing basic templating, is it fair to say that ES6 template strings could replace your current templating engine?

like image 592
wikiwong Avatar asked Dec 03 '15 05:12

wikiwong


People also ask

Are templating engines necessary?

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

Should I use a template engine or react?

I would consider building react component libraries react front-end development. If your talking about tempting engines in node app if you are taking about node. js then yes it's necessary as you need several side rendering then you have to use a templating engine on the server.

What is an advantage to using a templating engine?

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.

What is a JavaScript templating engine?

JavaScript templating engines enable you to add dynamic logic to static HTML pages. For instance, you can declare a variable that the engine replaces with an actual value at runtime. Similarly, you can use conditionals, loops, filters, mixins, and other constructs, depending on the templating engine you choose.

How hard is it to learn JavaScript templating?

Each engine comes with its own syntax that you need to learn. However, the learning curve is usually pretty flat, as template syntaxes are purposefully straightforward and easy to memorize. In this article, we have collected seven JavaScript templating engines you can use in your everyday workflow.

Should jQuery be removed from ES6 code?

A lot of of ES6 purists want to remove jQuery entirely, but as you said once someone starts going beyond a few one-off replacements, they often want to re-organize your ES6 code into something more efficient, the discussion will certainly focus on writing a wrapper library or utility functions for the ES6 code.

What are ‘template literals’ in ES2015?

For example: ES2015 introduced ‘Template Literals’. They allow us to do this instead: The back ticks define the contents as a template literal and then we can interpolate values from elsewhere in JavaScript using the $ {} pattern. Anything inside the curly braces, which in turn are within the backticks, are evaluated and then inserted as a string.


1 Answers

Template strings in ES6 aren't really related to the various template engines that are implemented in JavaScript.

Most template engines (Underscore, Lodash, Mustache, Handlebars, Jade, etc) all have special syntaxes and special forms. Some might give you the ability to define blocks, use special mark up for various things, or giving you unique tags for logic/looping structures.

ES6 Template Strings give you the full power of JavaScript without asking you to learn a specialized/opinionated template engine.

// underscore
var compiled = _.template("hello: <%= name %>");
compiled({name: 'moe'});
// => "hello: moe"

// ES6 Template String
let name = 'moe';
`hello: ${name}`;
// => "hello: moe"

Notice the ugly <%= %> tags in the underscore template? That's just something underscore invented to solve a "problem"; the "problem" being that before ES6, JavaScript didn't have any sort of string interpolation. People thought it was tedious to write things like

var greeting = "hello";
var name     = "moe";
var emotion  = "depressed";

greeting + ", my name is " + name + ". I feel " + emotion + ".";
// => "hello, my name is moe. I feel depressed."

With ES6, JavaScript gets native string interpolation via ${...}. Pretty much anything can go inside ${} as long as it's valid JavaScript.

let name = "alice";
let emotion = "happy";
`${greeting || "hi"}, my name is ${name}. I feel ${emotion}.`
// => "hi, my name is alice. I feel happy."
like image 109
Mulan Avatar answered Oct 04 '22 20:10

Mulan