So what are Template Literals? How can they do what a complex library like Handlebars does? Settle in, dear reader, and let’s find out… A crash course through Template Literals
![pugjs es literals pugjs es literals](https://raw.githubusercontent.com/bradlc/vscode-tailwindcss/master/packages/vscode-tailwindcss/.github/linting.png)
Ben suggested it’d be a good idea to turn our discussion into a post, and here we are - with a click bait title and everything. I had a recent discussion with one of the awesome developers at the FT - We were looking at how viable it would be to replace a templating language, like Handlebars with ES6 Template Literals, in some manner. SetExample.forEach(item => console.ES6 Template Literals, the Handlebars killer? Keith Cirkel JavaScript Cyber ShepherdĮS6 ES6 Template Literals, the Handlebars killer? Iterate over the set and print its content Spread example no.5: Objects and concatenatingĬonst objFour = ]) Spread example no.4: Objects and cloning Concatenate arrays using spread operatorĬonst arrayFour = Spread example no.3: Arrays and concatenatingĬonst arrayOne = Ĭonst arrayTwo = Spread example no.2: Array and cloning Spread example no.1: Array and printing the contentĬonst arrOne = Just remember to use commas to separate the operators. There is no limit imposed by ES6 by default. Meaning, when you use it to pass arguments, concatenate arrays or anything else. You can use spread operator as many times as you want. It consists of three dots and the name of the variable you want to use, i.e.: the array or object literal, (. Luckily, the syntax of spread operator is simple, unlike of some other features of ES6. Spread operator also comes in handy when you want to concatenate arrays or object literals, and save the result into a new one (code example no.3 and no.5). Cloning with spread operator is quick and simple (code example no.2 & no.4). Another good use case for this ES6 feature is cloning arrays and objects. All you have to do is to use spread operator syntax and let JavaScript do the work for you. You don’t need to use things such as loops or length and size properties to know the number of items, or properties. With spread operator, this doesn’t matter. Spread operator can be very useful, for example, when you don’t know the concrete number of elements inside the array or object literal.
![pugjs es literals pugjs es literals](https://static.sitestack.cn/projects/pugjs-en/wxacode.jpeg)
This feature allows you to take the content of objects such as array or object literal and copy it without using any arguments. One small and handy goodie provided by ES6 specification of ECMAScript is spread operator.
#Pugjs es literals how to
Understand what these features do and how to work with them so you can start using them in your projects with absolute confidence.ĮS6, ES7, ES8 & Writing Modern JavaScript Part 1 (Scope, let, const, var).ĮS6, ES7, ES8 & Writing Modern JavaScript Part 2 (Template literals, Destructuring & Default Params).ĮS6, ES7, ES8 & Writing Modern JavaScript Part 4 (Includes, Pads, Loops & Maps).ĮS6, ES7, ES8 & Writing Modern JavaScript Part 5 (WeakMap, WeakSet and Export & Import).ĮS6, ES7, ES8 & Writing Modern JavaScript Part 6 (Arrow functions & Promises).ĮS6, ES7, ES8 & Writing Modern JavaScript Part 7 (Async/await & Classes). In this part, you will learn about four of them, spread operator, rest parameter, sets and object literal.
![pugjs es literals pugjs es literals](https://pughtml.com/assets/logo.png)
![pugjs es literals pugjs es literals](https://i0.wp.com/css-tricks.com/wp-content/uploads/2017/05/nunjucks.png)