ES6 Template Strings - Tagged Templates
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 - 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));
Developer | Ian Joubert |
Username | Studira |
Uploaded | November 10, 2022 |
Rating | 3 |
Size | 4,334 Kb |
Views | 24,288 |
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!
Name | Size |
Angular Directive - Tracking element width | 3,316 Kb |
Bleeding Scope Variables | 2,734 Kb |
Asynchronous Functions | 3,150 Kb |
Processing Asynchronous Data | 3,264 Kb |
Parsing Locale data from official source | 5,255 Kb |
Ionic Modal Directive | 2,613 Kb |
A Pen by Ian Joubert | 2,273 Kb |
Removing Characters from String in Array | 3,258 Kb |
Angular Test Template | 2,545 Kb |
Facebook Profile Pictures - Flag Over | 6,841 Kb |
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!
Name | Username | Size |
Vanilla modal window | Jasonhowmans | 3,554 Kb |
JQuery Validate checkbox group | Lunaman | 2,466 Kb |
Plotting Points with D3.js | Laurakelly | 31,996 Kb |
Sassy Buttons | Elyseholladay | 2,299 Kb |
Bubble animation | Ftabor | 6,565 Kb |
Experiments with Vertical Centering | KatieK2 | 3,924 Kb |
Example SVGZ Data URI | Joeyhoer | 2,981 Kb |
Smoke Shader - Frame Buffer | Omarshe7ta | 2,672 Kb |
Cloud upload | Jaflo | 2,774 Kb |
Bezier Animation with straight paths | Rhernando | 2,087 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!