Skip to content

Commit 92905fb

Browse files
committed
Add tagged templater literals
1 parent f42c3fd commit 92905fb

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

tagged-template-literals/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ES6 Tagged Template Literals
2+
3+
> [https://www.youtube.com/watch?v=c9j0avG5L4c](https://www.youtube.com/watch?v=c9j0avG5L4c)
4+
5+
Install [Node.js](https://nodejs.org/).
6+
7+
Then run `node index.js` to run the example.
8+
9+
Or `npm start` and go to http://localhost:9966 in the browser.

tagged-template-literals/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// var polar = false
2+
// var bears = `
3+
// ${polar ? 'Polar' : ''}
4+
// Brown
5+
// Grizzly
6+
// `
7+
// var out = `Bears: ${bears.split('\n').join(', ')}`
8+
// console.log(out)
9+
10+
// var bears = ['Polar', 'Brown', 'Grizzly']
11+
// var html = `<ul>
12+
// ${bears.map(function (bear) {
13+
// return `<li>${bear}</li>`
14+
// }).join('')}
15+
// </ul>`
16+
// console.log(html)
17+
18+
var bear = 'Grizzly'
19+
var createElement = html`<div>${bear}</div>`
20+
21+
function html (strings, expr1, expr2, expr3) {
22+
return function () {
23+
var el = document.createElement('div')
24+
el.textContent = expr1
25+
return el
26+
}
27+
}
28+
29+
document.body.appendChild(createElement())
30+

tagged-template-literals/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "tagged-template-literals",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "budo index.js",
8+
"test": "node test.js"
9+
},
10+
"author": "Kyle Robinson Young <[email protected]> (http://dontkry.com)",
11+
"license": "MIT",
12+
"devDependencies": {
13+
"budo": "^8.3.0"
14+
}
15+
}

0 commit comments

Comments
 (0)