# ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด(Template Literal)

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์ด๋ž€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด์—๋Š” var str = 'Hello ES6'์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€์œผ๋‚˜ ES6์—์„œ๋Š” ๋ฐฑํ‹ฑ(back-tick)์ด๋ผ๋Š” ๊ธฐํ˜ธ(`)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

const str = `Hello ES6`;

์œ„์™€ ๊ฐ™์ด ๋ฐฑํ‹ฑ์„ ์ด์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ๋ฌธ์ž์—ด์„ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ณ€์ˆ˜๋ฅผ ๋ฌธ์ž์—ด ์•ˆ์— ๋ฐ”๋กœ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ์ด์ ์ด ์ƒ๊น๋‹ˆ๋‹ค.

# ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ๋ฌธ์ž์—ด ์„ ์–ธํ•˜๊ธฐ

๊ธฐ์กด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฌธ์ž์—ด ์„ ์–ธ ๋ฐฉ์‹์ธ ์ž‘์€ ๋”ฐ์˜ดํ‘œ(')์˜ ๋ฌธ์ œ์ ์€ ์•„๋ž˜์™€ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค.

var str = 'Template literals are string literals allowing embedded expressions. \n' + 
'You can use multi-line strings and string interpolation features with them. \n' + 
'They were called "template strings" in prior editions of the ES2015 specification.';

์ž‘์€ ๋”ฐ์˜ดํ‘œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ธด ๋ฌธ์ž์—ด์„ ์„ ์–ธํ•˜๊ฒŒ ๋˜๋ฉด ์ž๋™์œผ๋กœ ๊ฐœํ–‰์ด ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ผ์ธ ๋ธŒ๋ ˆ์ด์ปค(line breaker)์ธ \n๋ฅผ ๊ฐœํ–‰ํ•  ๊ณณ ์ค‘๊ฐ„ ์ค‘๊ฐ„์— ์ถ”๊ฐ€ํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋ฌธ์žฅ์ด ๊ธธ๋ฉด ๊ธธ์ˆ˜๋ก +์™€ \n๋ฅผ ๊ณ„์† ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์ƒ๊น๋‹ˆ๋‹ค.

๋ฐฑํ‹ฑ์„ ์ด์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ์„ ์–ธํ•˜๊ฒŒ ๋˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๊ฐœํ–‰ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

const str = `Template literals are string literals allowing embedded expressions.
You can use multi-line strings and string interpolation features with them.
They were called "template strings" in prior editions of the ES2015 specification.`;

๊ทธ๋Ÿผ ๋ทฐ์—์„œ๋Š” ์œ„์˜ ๋ฌธ๋ฒ•์„ ์–ด๋–ป๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? CDN ๋ฐฉ์‹์œผ๋กœ ๋ทฐ๋ฅผ ์ ์šฉํ•  ๋•Œ ์ปดํฌ๋„ŒํŠธ์˜ template ์†์„ฑ์— ์ ์šฉํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๋ง์ž…๋‹ˆ๋‹ค.

Vue.component('my-cmp', {
  template: `
    <div>
      <h1>My Component</h1>
      <p>back-tick is useful</p>
    </div>
  `
});

# ๋ฌธ์ž์—ด ์ค‘๊ฐ„์— ๋ณ€์ˆ˜ ๋ฐ”๋กœ ๋Œ€์ž…ํ•˜๊ธฐ

๊ธฐ์กด ๋ฌธ์ž์—ด ์ •์˜ ๋ฐฉ์‹์—์„œ ๋˜ ๋ฒˆ๊ฑฐ๋กœ์› ๋˜ ๋ถ€๋ถ„์€ ๋ฐ”๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฌธ์ž์—ด๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์ด์—ˆ์Šต๋‹ˆ๋‹ค.

var language = 'Javascript';
var expression = 'I love ' + language + '!';
console.log(expression); // I love Javascript!

์œ„์™€ ๊ฐ™์ด ๋ฌธ์ž์—ด์— ํŠน์ • ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ ค๋ฉด +๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ์ค‘๊ฐ„์— ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์—ฐ๊ฒฐํ•ด์ค˜์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค.

ES6์—์„œ๋Š” ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์„ ์ด์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ„ํŽธํ•˜๊ฒŒ ๋ฌธ์ž์—ด๊ณผ ๋ณ€์ˆ˜๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

var language = 'Javascript';
var expression = `I love ${language}!`;
console.log(expression); // I love Javascript!

${}๋ฅผ ์ด์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐ„๋‹จํ•œ ์—ฐ์‚ฐ๋„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

var language = 'Javascript';
var expression = `I love ${language.split('').reverse().join('')}!`;
console.log(expression); // I love tpircsavaJ!

์œ„ ์ฝ”๋“œ๋Š” language์˜ ๋ฌธ์ž์—ด ์ˆœ์„œ๋ฅผ ์—ญ์œผ๋กœ ๋ฐ”๊พธ๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.