DEV Community

Kannan
Kannan

Posted on

1 1

Tagged Template Literals

Hello Everyone 👋,

Tagged template literals is an es6 feature which allow you to tag a template to a function.

If you are from react/graphql world you would have seen the below syntax.

const Button = styled.button`
color: blue;
`;
const userQuery = gql`
{
users {
firstName
}
}
`;

Even If you do not know this syntax follow along you will understand.

Here styled.button and gql are actually just functions.

See the below example

const generateIntro = (strings, ...values) => {
console.log(strings); // [ 'My name is ', ' and I am a ', '' ]
console.log(values); // [ 'Kannan', 'Javascript developer' ]
let str = '';
strings.forEach((string, i) => {
str += string + (values[i] || '');
});
return str;
}
const name = 'Kannan';
const role = 'Javascript developer';
generateIntro`My name is ${name} and I am a ${role}`;
// My name is Kannan and I am a Javascript developer

Whenever a tagged function is called the first argument will be array of strings which is split on the interpolation.

Notice that we have used second argument with rest operator because each interpolation value will be passed as an argument to the function. Imagine there are 10 interpolation in a string getting each argument separately is not a good practice.

This can be used to completely transform the string.

Consider a use case while rendering a block in the html all its dynamic values needs to be made bold. We can write a simple function with tagged template easily to preprocess the string for us.

const makeBold = (strings, ...values) => {
let str = '';
strings.forEach((string, i) => {
str += `${string} ${values[i] ? `<strong>${values[i]}<strong>` : ''}`;
});
return str;
}
const name = 'Kannan';
const role = 'Javascript developer';
makeBold`My name is ${name} and I am a ${role}`;
// My name is <strong>Kannan<strong> and I am a <strong>Javascript developer<strong>

Note: There will be always one value more in the strings array then the values.

Check out these awesome libraries that use tagged templates:
Styled-Components
Common-tags
i18n-tag
graphql-tag

Please like and share if you find this interesting.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay