← Back to JavaScript Course | Chapter 5: ES6+ Features | Lesson 10 of 12

JS टैग्ड टेम्पलेट्स

एक tagged template एक ऐसा function है जिसे टेक्स्ट के किसी blanks वाले टुकड़े को स्ट्रिंग बनने से पहले process करने का मौका मिलता है, जैसे कोई helper जो एक form letter को edit करता है। आप function का नाम backticks से ठीक पहले रखते हैं।
Syntax
javascript
function tag(strings, ...values) {
  // build and return a result
}

tag`text ${value} text`;

Tagged Template क्या है

एक tagged template किसी function के नाम से पहले आया एक template literal है; सीधे एक स्ट्रिंग बनाने के बजाय, JavaScript custom processing के लिए literal के टुकड़ों के साथ उस function को कॉल करता है।

उदाहरण: What Is a Tagged Template

javascript
// Define the function `tag` taking `strings`, `value`
// Define the function `tag` taking `strings`, `value`
function tag(strings, value) {
  // Print `strings, value` to the console
  // Print `strings, value` to the console
  console.log(strings, value);
}
// Declare the constant `name`, set to "Sam"
// Declare the constant `name`, set to "Sam"
const name = "Sam";
tag`Hello ${name}`;

Tag Function के Arguments

Tag function को अपने पहले argument के रूप में literal के string segments की एक array मिलती है, उसके बाद हर interpolated ${} value अलग अतिरिक्त arguments के रूप में, जो उसे fixed text और dynamic टुकड़ों दोनों तक पूरी पहुंच देती है।

उदाहरण: Tag Function Arguments

javascript
// Define the function `tag` taking `strings`, `...values`
// Define the function `tag` taking `strings`, `...values`
function tag(strings, ...values) {
  // Print `strings` to the console
  // Print `strings` to the console
  console.log(strings);
  // Print `values` to the console
  // Print `values` to the console
  console.log(values);
}
tag`A ${1} B ${2}`;

Tags से Formatting

चूंकि आप नियंत्रित करते हैं कि टुकड़े कैसे मिलाए जाते हैं, एक tag function HTML escape कर सकता है, currency format कर सकता है, substitutions को highlight कर सकता है, या आखिरी स्ट्रिंग बनाने से पहले कोई भी custom transformation लगा सकता है।

उदाहरण: Formatting with Tags

javascript
// Define the function `highlight` taking `strings`, `...values`
// Define the function `highlight` taking `strings`, `...values`
function highlight(strings, ...values) {
  // Return `strings.reduce((out, str, i) => `${out}${str}${values[i] ? `[${values[i]}]` : ""}`, "")` from this function
  // Return `strings.reduce((out, str, i) => `${out}${str}${values[i] ? `[${values[i]}]` : ""}`, "")` from this function
  return strings.reduce((out, str, i) => `${out}${str}${values[i] ? `[${values[i]}]` : ""}`, "");
}
// Print `highlight`Score: ${95}`` to the console
// Print `highlight`Score: ${95}`` to the console
console.log(highlight`Score: ${95}`);

Reusable Tag Functions

एक tag function बस एक normal function है, इसलिए आप इसे एक बार लिखकर उन कई template literals में दोबारा इस्तेमाल कर सकते हैं जहाँ भी वह खास formatting या escaping व्यवहार चाहिए।

उदाहरण: Reusable Tag Functions

javascript
// Define the function `upper` taking `strings`, `...values`
// Define the function `upper` taking `strings`, `...values`
function upper(strings, ...values) {
  // Return `strings.reduce((out, str, i) => out + str + (values[i] ?? "").toString().toUpperCase(), "")` from this function
  // Return `strings.reduce((out, str, i) => out + str + (values[i] ?? "").toString().toUpperCase(), "")` from this function
  return strings.reduce((out, str, i) => out + str + (values[i] ?? "").toString().toUpperCase(), "");
}
// Print `upper`Hello ${"world"}`` to the console
// Print `upper`Hello ${"world"}`` to the console
console.log(upper`Hello ${"world"}`);
// Print `upper`Bye ${"friend"}`` to the console
// Print `upper`Bye ${"friend"}`` to the console
console.log(upper`Bye ${"friend"}`);

Tagged Template के Use Cases

Tagged templates उन असली libraries को चलाते हैं जिन्हें आप शायद पहले से इस्तेमाल करते हैं, जैसे styled-components का CSS-in-JS syntax और SQL-templating libraries जो interpolated values को सुरक्षित रूप से escape करती हैं।

उदाहरण: Tagged Template Use Cases

javascript
// Real-world tagged templates (illustrative):
// styled.div`color: ${theme.color};`
// sql`SELECT * FROM users WHERE id = ${userId}`
console.log("Tagged templates power CSS-in-JS and safe SQL templating.");
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि tag function पहले strings की एक array लेता है और बाद में values, इसलिए argument order गलत हो जाता है।
  2. parentheses के साथ tag("text") लिखना जब आप ` tagtext ` चाहते थे, जो strings और values को अलग-अलग pass नहीं करता।
  3. tag function से कुछ return न करना, इसलिए result undefined होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.