-
in nodejs main.js: let template = "Handlebars <b>{{doesWhat}}</b> precompiled!";
let Handlebars = require("handlebars");
let compiled = Handlebars.precompile(template);
const fs = require('fs');
fs.writeFileSync("b", compiled); works ok. {"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
var helper, lookupProperty = container.lookupProperty || function(parent, propertyName) {
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
return parent[propertyName];
}
return undefined
};
return "Handlebars <b>"
+ container.escapeExpression(((helper = (helper = lookupProperty(helpers,"doesWhat") || (depth0 != null ? lookupProperty(depth0,"doesWhat") : depth0)) != null ? helper : container.hooks.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),{"name":"doesWhat","hash":{},"data":data,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":26}}}) : helper)))
+ "</b> precompiled!";
},"useData":true} client.js: const fs = require('fs');
const data = fs.readFileSync('./b',
{ encoding: 'utf8', flag: 'r' });
let Handlebars = require("handlebars");
Handlebars.partials["test1"] = Handlebars.template(data);
var result = Handlebars.partials["test1"]({ name: "yourname" });
console.log(result); code is from https://handlebarsjs.com/installation/precompilation.html#precompiling-templates-inside-nodejs results:
I try to speed up my program, so I try to reuse compiled template. can't find a way to do it. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
chatgptYou: main.js: let template = "Handlebars <b>{{doesWhat}}</b> precompiled!";
let Handlebars = require("handlebars");
let compiled = Handlebars.precompile(template);
const fs = require('fs');
fs.writeFileSync("b", compiled); works ok. {"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
var helper, lookupProperty = container.lookupProperty || function(parent, propertyName) {
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
return parent[propertyName];
}
return undefined
};
return "Handlebars <b>"
+ container.escapeExpression(((helper = (helper = lookupProperty(helpers,"doesWhat") || (depth0 != null ? lookupProperty(depth0,"doesWhat") : depth0)) != null ? helper : container.hooks.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),{"name":"doesWhat","hash":{},"data":data,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":26}}}) : helper)))
+ "</b> precompiled!";
},"useData":true} client.js: const fs = require('fs');
const data = fs.readFileSync('./b',
{ encoding: 'utf8', flag: 'r' });
let Handlebars = require("handlebars");
Handlebars.partials["test1"] = Handlebars.template(data);
var result = Handlebars.partials["test1"]({ name: "yourname" });
console.log(result); code is from https://handlebarsjs.com/installation/precompilation.html#precompiling-templates-inside-nodejs results:
I try to speed up my program, so I try to reuse compiled template. can't find a way to do it. ChatGPT: Solution:Instead of directly writing and reading the compiled template as a string, you need to evaluate the string in Fixed
|
Beta Was this translation helpful? Give feedback.
chatgpt
You:
in nodejs
main.js:
works ok.
I get this in
b
: