Hacker News new | ask | show | jobs
by latchkey 5024 days ago
This posting seems like basic knowledge, but I can see how people get lazy and don't do this step as part of their build process and just send things to the browser to do it for them.

Part of the issue is that the default bin/handlebars script doesn't really support walking a tree and outputting a directory structure with all of the precompiled templates, but I've got my own hacked version of it which does...

https://gist.github.com/3719225

  handlebars ./handlebars --min --outputDir ./js/tmpl
I also have a build script setup in Eclipse so that when I save the file, it automatically builds things... (similar to this)...

http://stackoverflow.com/questions/6645640/integrating-coffe...

I use requirejs with a paths configuration like this:

  handlebars: 'handlebars.runtime-1.0.0.beta.6'
This allows me to just write this in my CoffeeScript for each page on my site...

  require('handlebars')
  require('tmpl/org/requests')
  ...
  
  requests.html(Handlebars.templates.org_requests(requests: requests)
All of this works amazingly well and has really allowed me to segment my code and templates up into little sections for reusability. Also, no need for ever loading the compiler part of handlebars in the client even during development.
1 comments

Judging by the response, it appears to not be basic knowledge. But, I'm glad to hear that you and others are taking a similar approach as I think it just makes a lot more sense as a concept.
Sorry, I meant the part about the requirement to precompile templates in order to have the fastest possible experience.
It's common sense once someone explains it to you, but when you're learning about all of this new client-side tech you tend not to worry about speed until later, and then it's useful to see posts like these telling you what you can do.

I guess this is why frameworks like Rails compile your assets for you - it's a "silly not to do it" task, but not everyone knows about it.

Right on the homepage of handlebarsjs.com...

"It is also possible to precompile your templates. This will result in a smaller required runtime library and significant savings from not having to compile the template in the browser. This can be especially important when working with mobile devices."

But nobody reads documentation... ;-)