ES6 Template Strings - Tagged Templates

Developer
Size
4,334 Kb
Views
24,288

How do I make an es6 template strings - tagged templates?

Template Strings use back-ticks (``) rather than the single or double quotes we're used to with regular strings. Following is an illustration of not only Template Strings but also an interesting use case for Tagged Templates.. What is a es6 template strings - tagged templates? How do you make a es6 template strings - tagged templates? This script and codes were developed by Ian Joubert on 10 November 2022, Thursday.

ES6 Template Strings - Tagged Templates Previews

ES6 Template Strings - Tagged Templates - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>ES6 Template Strings - Tagged Templates</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <link href='https://fonts.googleapis.com/css?family=Berkshire+Swash' rel='stylesheet' type='text/css'>
<div class="container">	<img src="https://s.gravatar.com/avatar/004182992aa38d718fabcee7d643cec2?s=80"/>	<article layout="column" layout-align="center center" flex>	<h1 class="title">ES6 Literal Template Strings</h1>	<small>This Codepen was developed by: Ian Joubert</small>	<h3>Formatting a string message with ES6 tagged templates</h3>	<p>This pen is to illustrate the awesome ES6 template Strings ( ` back-tick). In the code I parse a data object into a set message format by running the string template through a parser function. The function receives as first argument the pieces (in order) of the template before each substituted data property. The function then manipulates the pieces and data to form a nicely formatted message.</p>	<p>Open your console to see the result!</p>	</article>
</div> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

ES6 Template Strings - Tagged Templates - Script Codes CSS Codes

body { font-family: Sans-serif;
}
.container { text-align: center;
}
.title { font: 700 3rem Sans-serif; color: #feda44; text-shadow: 1px 2px 0px rgba(0, 0, 0, 0.25); margin: 0;
}
article h3 { color: rgba(0, 0, 0, 0.4); margin: 8px 0 4px;
}
article small { color: rgba(0, 0, 0, 0.2);
}
article p { color: rgba(0, 0, 0, 0.4); text-align: center; line-height: 1.2em;
}

ES6 Template Strings - Tagged Templates - Script Codes JS Codes

'use strict';
var _templateObject = _taggedTemplateLiteralLoose(['Post posted by ', 'Comment: ', 'Comment: ', 'Date:', ', at Time:', ''], ['Post posted by ', 'Comment: ', 'Comment: ', 'Date:', ', at Time:', '']);
function _taggedTemplateLiteralLoose(strings, raw) { strings.raw = raw; return strings; }
// Tagged Templates transform a Template String by placing a function name before the template string.
// For example:
var data = {	id: '123456',	comment: undefined,	comment2: 'This comment will be parsed because its not "undefined"',	category: 'Some weird category',	registration: '',	date: '22, June 2016',	time: '22h23',	user: 'Ian Joubert'
};
function parser(pieces) {	// Changed "pieces" argument into a new array. In strict mode deleting or adding to it	// directly will fail with a type error because the object is sealed.	var newPieces = [].concat(pieces);	var result = newPieces[0];	// Same issue with the arguments object - see above	var subs = _.drop([].concat(Array.prototype.slice.call(arguments)));	console.log(newPieces);	console.log(subs);	subs = _.filter(subs, function (value, index) {	if (_.isUndefined(value)) {	_.pullAt(newPieces, index);	}	return !_.isUndefined(value);	});	for (var i = 0; i < subs.length; ++i) {	result += subs[i] + newPieces[i + 1];	}	return result;
}
// Using tagged template to parse a string into a custom formatted message string
console.log(parser(_templateObject, data.user, data.comment, data.comment2, data.date, data.time));
ES6 Template Strings - Tagged Templates - Script Codes
ES6 Template Strings - Tagged Templates - Script Codes
Home Page Home
Developer Ian Joubert
Username Studira
Uploaded November 10, 2022
Rating 3
Size 4,334 Kb
Views 24,288
Do you need developer help for ES6 Template Strings - Tagged Templates?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Ian Joubert (Studira) Script Codes
Create amazing sales emails with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!