New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error: Unknown template object: string when using a precompiled template with .template() #922
Comments
The |
Thanks for the answer. It seems the docs are more geared towards using Handlebars in the client side, since it is possible to pass just the uncompiled partial string to the |
I am writing a server-side application that needs to be able to both precompile templates (and then deliver them to the client in case they need to be re-rendered with new context) and also render them to HTML (for less noticeable flicker/delay on the client + improved SEO). It seems I either need to call both In case anyone else got confused by the documentation for precompile and wound up here, I've written an example to illustrate what is going on. (Actually, I wrote it because I was trying to figure it out what I was doing wrong.) 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 you'll want to call compile and precompile. The generated code for the two is very different and I wouldn't presume that trying to toString or similar will work with the compile output, etc. |
I'm trying to register a partial, which according to the docs should be done like this:
However the
.template()
method produces this error:I can see that the
compiledPartial
above is indeed a string that looks like an object:WTF?
Node 0.10.25, Handlebars 2.0.0
The text was updated successfully, but these errors were encountered: