Skip to content

Instantly share code, notes, and snippets.

@davidnormo
Created July 15, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidnormo/6767256c798b6adb3b3d to your computer and use it in GitHub Desktop.
Save davidnormo/6767256c798b6adb3b3d to your computer and use it in GitHub Desktop.
es6-tagged-template-strings.js
let asAttrs = (strings, value) => {
let attrs = [];
for (let prop in value) {
let val = value[prop];
if (typeof val === 'boolean'){
attrs.push(`${prop}` + (val ? '' : '=false'));
} else {
attrs.push(`${prop}="${val}"`);
}
}
return attrs.join(' ');
}
let props = {
'data-one': 'one',
b: true,
c: false
};
let tag = 'div';
let attrs = `<${tag} ` + asAttrs`${props}` + `>Hello world!</${tag}>`;
console.log(attrs); // '<div data-one="one" b c=false>Hello world!</div>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment