Handlebars.js: 였λ₯˜: μ•Œ 수 μ—†λŠ” ν…œν”Œλ¦Ώ 개체: .template()κ³Ό ν•¨κ»˜ 미리 컴파일된 ν…œν”Œλ¦Ώμ„ μ‚¬μš©ν•  λ•Œ λ¬Έμžμ—΄

에 λ§Œλ“  2014λ…„ 12μ›” 12일  Β·  4μ½”λ©˜νŠΈ  Β·  좜처: handlebars-lang/handlebars.js

λ¬Έμ„œμ— λ”°λ₯΄λ©΄ λ‹€μŒκ³Ό 같이 μˆ˜ν–‰ν•΄μ•Ό ν•˜λŠ” 뢀뢄을 λ“±λ‘ν•˜λ €κ³  ν•©λ‹ˆλ‹€.

var partialString = fs.readFileSync(filename), {encoding: 'utf8'})
var compiledPartial = handlebars.precompile(partialString)
var template = handlebars.template(compiledPartial)

handlebars.registerPartial('stuff', template)

κ·ΈλŸ¬λ‚˜ .template() λ©”μ„œλ“œλŠ” λ‹€μŒ 였λ₯˜λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

Error: Unknown template object: string

μœ„μ˜ compiledPartial κ°€ μ‹€μ œλ‘œ 객체처럼 λ³΄μ΄λŠ” λ¬Έμžμ—΄μž„μ„ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

{"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
return "<div>Stuff</div>\n";
},"useData":true}

WTF?

λ…Έλ“œ 0.10.25, ν•Έλ“€λ°” 2.0.0

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

μ €λŠ” ν…œν”Œλ¦Ώμ„ 미리 컴파일(그리고 μƒˆ μ»¨ν…μŠ€νŠΈλ‘œ λ‹€μ‹œ λ Œλ”λ§ν•΄μ•Ό ν•˜λŠ” 경우 ν΄λΌμ΄μ–ΈνŠΈμ— 전달)ν•˜κ³  HTML둜 λ Œλ”λ§(λˆˆμ— 띄지 μ•ŠλŠ” κΉœλ°•μž„/ ν΄λΌμ΄μ–ΈνŠΈ 지연 + SEO κ°œμ„ ). precompile 및 compile λ³„λ„λ‘œ ν˜ΈμΆœν•˜κ±°λ‚˜ μ§λ ¬ν™”λœ 사전 컴파일러 좜λ ₯을 eval ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. μ΄λŸ¬ν•œ μ ‘κ·Ό 방식 쀑 μ–΄λŠ 것도 맀우 μš°μ•„ν•˜κ²Œ λŠκ»΄μ§€μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. κ·Έλž˜μ„œ μ œκ°€ λ­”κ°€λ₯Ό λ†“μΉ˜κ³  μžˆμŠ΅λ‹ˆκΉŒ? μ•„λ‹ˆλ©΄ 제 μ‘μš© ν”„λ‘œκ·Έλž¨μ΄ μ΄μƒν•œκ°€μš”? (BTW, 이 μ‘μš© ν”„λ‘œκ·Έλž¨μ€ Java둜 μž‘μ„±λ˜μ—ˆμ§€λ§Œ ν…œν”Œλ¦Ώ 뢀뢄에 λ‚΄μž₯된 [Rhino/Nashorn] JavaScript 엔진을 μ‚¬μš©ν•©λ‹ˆλ‹€. λ¬Έμ œκ°€ λ˜μ§€ μ•ŠκΈ°λ₯Ό λ°”λžλ‹ˆλ‹€.)

λ‹€λ₯Έ μ‚¬λžŒμ΄ ν”„λ¦¬μ»΄νŒŒμΌ 에 λŒ€ν•œ

var templateSources = {
    hi: "Hi, {{name}}.",
    bye: "Goodbye, {{name}}."
};
var compiledTemplates = {};
var serializedTemplates = {};
var deserializedTemplates = {};

Object.keys(templateSources).forEach(function(name) {
    // Use compile method to generate actual executable template (function)
    compiledTemplates[name] = Handlebars.compile(templateSources[name]);
    // Use precompile method to generate serialized JS (string)
    serializedTemplates[name] = Handlebars.precompile(templateSources[name]);
    // If we really want, we can deserialize these 
    deserializedTemplates[name] = Handlebars.template(evalPrecompiledTemplate(serializedTemplates[name]));
});

// (Yes, I know eval is dangerous)
function evalPrecompiledTemplate(s) {
    return eval("(function(){return " + s + "}());");
}

// Quick demonstration that these template functions work the same
var context = {
    name: "John Smith"
};

// Output: 
// Rendering template named hi with context: Object {name: "John Smith"}
// Hi, John Smith.(compiled)
// Hi, John Smith.(precompiled/deserialized)
// Rendering template named bye with context: Object {name: "John Smith"}
// Goodbye, John Smith.(compiled)
// Goodbye, John Smith.(precompiled/deserialized)
Object.keys(templateSources).forEach(function(name) {
  console.log("Rendering template named " + name + " with context:", context);
  console.log(compiledTemplates[name](context) + "(compiled)");
  console.log(deserializedTemplates[name](context) + "(precompiled/deserialized)");
});

λͺ¨λ“  4 λŒ“κΈ€

precompiler λ©”μ„œλ“œλŠ” ν…œν”Œλ¦Ώμ„ μ§λ ¬ν™”ν•˜κΈ° μœ„ν•œ 것이며 template λ©”μ„œλ“œλŠ” ν΄λΌμ΄μ–ΈνŠΈμ—μ„œ ν…œν”Œλ¦Ώμ„ λ³΅μ›ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. λ™μΌν•œ ν”„λ‘œμ„ΈμŠ€μ—μ„œ ν…œν”Œλ¦Ώμ„ ν‰κ°€ν•˜λ €λŠ” 경우 Handlebars.compile 직접 μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

λ‹΅λ³€ ν•΄μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€. μ„œλ²„ μΈ‘μ—μ„œ ν•Έλ“€λ°”λ§Œ μ‚¬μš©ν•˜λŠ” 경우 μ»΄νŒŒμΌλ˜μ§€ μ•Šμ€ λΆ€λΆ„ λ¬Έμžμ—΄ registerPartial() λ©”μ„œλ“œμ— 전달할 수 있기 λ•Œλ¬Έμ— λ¬Έμ„œλŠ” ν΄λΌμ΄μ–ΈνŠΈ μΈ‘μ—μ„œ ν•Έλ“€λ°”λ₯Ό μ‚¬μš©ν•˜λŠ” 데 더 μ ν•©ν•©λ‹ˆλ‹€.

μ €λŠ” ν…œν”Œλ¦Ώμ„ 미리 컴파일(그리고 μƒˆ μ»¨ν…μŠ€νŠΈλ‘œ λ‹€μ‹œ λ Œλ”λ§ν•΄μ•Ό ν•˜λŠ” 경우 ν΄λΌμ΄μ–ΈνŠΈμ— 전달)ν•˜κ³  HTML둜 λ Œλ”λ§(λˆˆμ— 띄지 μ•ŠλŠ” κΉœλ°•μž„/ ν΄λΌμ΄μ–ΈνŠΈ 지연 + SEO κ°œμ„ ). precompile 및 compile λ³„λ„λ‘œ ν˜ΈμΆœν•˜κ±°λ‚˜ μ§λ ¬ν™”λœ 사전 컴파일러 좜λ ₯을 eval ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. μ΄λŸ¬ν•œ μ ‘κ·Ό 방식 쀑 μ–΄λŠ 것도 맀우 μš°μ•„ν•˜κ²Œ λŠκ»΄μ§€μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. κ·Έλž˜μ„œ μ œκ°€ λ­”κ°€λ₯Ό λ†“μΉ˜κ³  μžˆμŠ΅λ‹ˆκΉŒ? μ•„λ‹ˆλ©΄ 제 μ‘μš© ν”„λ‘œκ·Έλž¨μ΄ μ΄μƒν•œκ°€μš”? (BTW, 이 μ‘μš© ν”„λ‘œκ·Έλž¨μ€ Java둜 μž‘μ„±λ˜μ—ˆμ§€λ§Œ ν…œν”Œλ¦Ώ 뢀뢄에 λ‚΄μž₯된 [Rhino/Nashorn] JavaScript 엔진을 μ‚¬μš©ν•©λ‹ˆλ‹€. λ¬Έμ œκ°€ λ˜μ§€ μ•ŠκΈ°λ₯Ό λ°”λžλ‹ˆλ‹€.)

λ‹€λ₯Έ μ‚¬λžŒμ΄ ν”„λ¦¬μ»΄νŒŒμΌ 에 λŒ€ν•œ

var templateSources = {
    hi: "Hi, {{name}}.",
    bye: "Goodbye, {{name}}."
};
var compiledTemplates = {};
var serializedTemplates = {};
var deserializedTemplates = {};

Object.keys(templateSources).forEach(function(name) {
    // Use compile method to generate actual executable template (function)
    compiledTemplates[name] = Handlebars.compile(templateSources[name]);
    // Use precompile method to generate serialized JS (string)
    serializedTemplates[name] = Handlebars.precompile(templateSources[name]);
    // If we really want, we can deserialize these 
    deserializedTemplates[name] = Handlebars.template(evalPrecompiledTemplate(serializedTemplates[name]));
});

// (Yes, I know eval is dangerous)
function evalPrecompiledTemplate(s) {
    return eval("(function(){return " + s + "}());");
}

// Quick demonstration that these template functions work the same
var context = {
    name: "John Smith"
};

// Output: 
// Rendering template named hi with context: Object {name: "John Smith"}
// Hi, John Smith.(compiled)
// Hi, John Smith.(precompiled/deserialized)
// Rendering template named bye with context: Object {name: "John Smith"}
// Goodbye, John Smith.(compiled)
// Goodbye, John Smith.(precompiled/deserialized)
Object.keys(templateSources).forEach(function(name) {
  console.log("Rendering template named " + name + " with context:", context);
  console.log(compiledTemplates[name](context) + "(compiled)");
  console.log(deserializedTemplates[name](context) + "(precompiled/deserialized)");
});

@jacobq 컴파일 및 사전 μ»΄νŒŒμΌμ„ ν˜ΈμΆœν•˜κ³  싢을 κ²ƒμž…λ‹ˆλ‹€. λ‘˜μ— λŒ€ν•΄ μƒμ„±λœ μ½”λ“œλŠ” 맀우 λ‹€λ₯΄λ©° toString λ˜λŠ” 이와 μœ μ‚¬ν•œ 것을 μ‹œλ„ν•˜λŠ” 것이 컴파일 좜λ ₯ λ“±μ—μ„œ μž‘λ™ν•œλ‹€κ³  κ°€μ •ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰