λ¬Έμμ λ°λ₯΄λ©΄ λ€μκ³Ό κ°μ΄ μνν΄μΌ νλ λΆλΆμ λ±λ‘νλ €κ³ ν©λλ€.
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
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 λλ μ΄μ μ μ¬ν κ²μ μλνλ κ²μ΄ μ»΄νμΌ μΆλ ₯ λ±μμ μλνλ€κ³ κ°μ νμ§ μμ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
μ λ ν νλ¦Ώμ 미리 μ»΄νμΌ(κ·Έλ¦¬κ³ μ 컨ν μ€νΈλ‘ λ€μ λ λλ§ν΄μΌ νλ κ²½μ° ν΄λΌμ΄μΈνΈμ μ λ¬)νκ³ HTMLλ‘ λ λλ§(λμ λμ§ μλ κΉλ°μ/ ν΄λΌμ΄μΈνΈ μ§μ° + SEO κ°μ ).
precompile
λ°compile
λ³λλ‘ νΈμΆνκ±°λ μ§λ ¬νλ μ¬μ μ»΄νμΌλ¬ μΆλ ₯μeval
νλ κ² κ°μ΅λλ€. μ΄λ¬ν μ κ·Ό λ°©μ μ€ μ΄λ κ²λ λ§€μ° μ°μνκ² λκ»΄μ§μ§ μμ΅λλ€. κ·Έλμ μ κ° λκ°λ₯Ό λμΉκ³ μμ΅λκΉ? μλλ©΄ μ μμ© νλ‘κ·Έλ¨μ΄ μ΄μνκ°μ? (BTW, μ΄ μμ© νλ‘κ·Έλ¨μ Javaλ‘ μμ±λμμ§λ§ ν νλ¦Ώ λΆλΆμ λ΄μ₯λ [Rhino/Nashorn] JavaScript μμ§μ μ¬μ©ν©λλ€. λ¬Έμ κ° λμ§ μκΈ°λ₯Ό λ°λλλ€.)λ€λ₯Έ μ¬λμ΄ ν리컴νμΌ μ λν