-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Loading precompiled templates server side (Node JS) #955
Comments
This isn't enough information to reproduce. Can you provide an full example of what you are trying to do? |
Sure, here we go.
**** hello.html ****
I renamed the file above as hello.handlebars and compiled it to hello.js, put it in a directory called “templates” below my “js” directory. So simply trying to load "precompiled" template to produce dynamic pages on server side. |
Precompiled templates need to be compiled at some point and generally are not intended for use on servers as they require additional steps to work with them. Your first version is the recommended route, although I believe you can do something like
|
Pardon me I don't understand "Precompiled templates need to be compiled". Precompiled means they are already compiled. Right ? Handlebar templates get compiled to a .js file and then that is filled with db data or context to generate the final page. Correct ? So I am just trying to precompile for speed so I can dynamically generate page off course feeding them with my db data at run time on the server side for final transmission.as "text/html" to client browser. My question simply is how to load the 'hello' template from hello.js created from hello,handlebar so I have a compiled template object ? Or say another template called "hello2" from a aggregate templates.js file containing all my compiled templates. By the way I tried require, but that does not work. Apologize, if I being senile. |
Precompiled means that they are precompiled by you for your clients. They are not made available automatically to node consumers. Your example using readFileSync is the easiest and likely the best way to load the templates, so you are on the right path there. |
@RozzyNoder Hi I'm trying to do something similar to this. Did you manage to make it work? Every time I take the outputted precompiled templates to a file and then later try to read them into a variable the json parsing fails because json technically isn't supposed to have functions in them but of course the hbs templates are functions. I've also tried a dead simple example, one from the handlebars site itself
and i get an error like: incidentally, this is run via gulp 3.9.0, and handlebars 3.0.3. I would love any and all insight you might have. I'm really stuck and I need this to work very soon (like as in tomorrow) Thanks in advance. |
@kpdecker I try to achieve the same as described by @ekskimn . I generate HTML code in a loop so I guess precompiling a template would speed up the process. What do you suggest? I should also mention that I found a post somewhere (can't remember where) saying that we need to precompile templates to ensue XSS security on the server in case we need to handle templates provided by the user (user-input). |
@xpepermint if you have a loop, from what I know, it should be performant to do something like: var template = Handlebars.compile("{{title}}\n{{text}}");
largeArray.map(function(element) {
return template(element);
}); Just compile the template before the loop and call the compiled function inside the loop. |
@nknapp thx |
Did anyone manage to use precompiled templates server side? |
I also wanted to use precompiled templates server side. I am using a lambda function to send emails with HTML content, so I wanted to minimize my function's execution time and package size ( I have a script that generates precompiled templates ( It is something like: const Handlebars = require('handlebars')
const precompiledContent = Handlebars.precompile('<b>{{ message }}</b>')
fs.writeFileSync('xyz/template.js', `module.exports = ${precompiledContent}`, 'utf8') Then you can use the template like this:
|
@RozzyNoder @ekskimn I managed to make this work but with a slight change. Instead of your reference to
refer the actual hbs file that is used to generate the .js file, i.e.
|
Every time I try to load precompiled templates like below:
var compiledTemplate = handlebars.templates['hello'];
I get a cannot read property 'hello' of undefined.
whereas the compile works fine like below:
var uncompiledTemplate = handlebars.compile(template);
So what is the protocol or configuration for loading these compiled templates so they get read in from the templates directory/file ?
I checked my configuration, file names, the handlebar is version (2.0 beta) and compiler version is six.
Thanks.
The text was updated successfully, but these errors were encountered: