Skip to content

Instantly share code, notes, and snippets.

@pbzona
Last active September 4, 2017 23:40
Show Gist options
  • Save pbzona/c283bc26f36e5384e15a6aca3c4b2e95 to your computer and use it in GitHub Desktop.
Save pbzona/c283bc26f36e5384e15a6aca3c4b2e95 to your computer and use it in GitHub Desktop.
Template tag example 3
function someTemplate(strings, personName, favoriteFood) {
var output = strings[0] + personName + strings[1] + favoriteFood;
if (favoriteFood.toLowerCase() === 'pizza') {
return output + '. HELL YEAH!!!';
} else if (favoriteFood.toLowerCase() === 'broccoli') {
return output + '. GROSS!';
} else {
return output + strings[2];
}
}
var name = 'Phil';
var foodOne = 'Pizza';
var resultOne = someTemplate`Your name is ${name} and your favorite food is ${foodOne}. Well, okay.`;
var foodTwo = 'Broccoli';
var resultTwo = someTemplate`Your name is ${name} and your favorite food is ${foodTwo}. Well, okay.`;
var foodThree = 'Dumplings';
var resultThree = someTemplate`Your name is ${name} and your favorite food is ${foodThree}. Well, okay.`;
console.log(resultOne);
console.log(resultTwo);
console.log(resultThree);
// Your name is Phil and your favorite food is Pizza. HELL YEAH!!!
// Your name is Phil and your favorite food is Broccoli. GROSS!
// Your name is Phil and your favorite food is Dumplings. Well, okay.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment