ES2015 templates

Ok, you caught me, today I'm giving myself an easy one this time. :)

ES2015 templates are still pretty neat. Before this feature, people would use string concatenation or have to import a templating library for simple stuff like this.

Ok, here's an example:

var mad_libs = (o) =>  
    `${o.person1} should ${o.verb} ${o.person2} ${o.thing}.`

        person1: 'Your potato',
        verb: 'email',
        person2: 'that pancake',
        thing: 'the blueberry pie manifesto'

This gives us the output:

Your potato should email that pancake the blueberry pie manifesto.  

ES2015 templates allow you to have customizable string output without having to resort to using the + operator to join a bunch of strings together.

Instead of single or double quotes, you use ticks, like this one: `

The tick character is usually the same key on your keyboard that gives you the tilde ~

If you want to include a variable in your template, just use a dollar sign and brackets: ${like_so}.

In our example above I'm using various properties of oto build a string.

Like I said, it's useful for little things, for building strings. But if you need a template for making markup, it's best to use a template language like pug/jade or mustache.

Looking for a software developer?