Has anyone found a good solution for developing templates for backbone.js that can be used on the server and the client?
This is really desirable with the backbone.js history stack, as users can share and link to real urls in the browser location bar, and the node.js server can render the page on first page view, while using the same templates in the client to rebuild pages on subsequent page views.
This would also provide an ideal output for both users and for search engines that spider the links and won't have to parse or execute javascript to see a fully rendered and working page.
Updated with more info:
Two possible approaches to this appear to be:
1) bones - https://github.com/developmentseed/bones
Bones has some quirks to install and currently needs an old version of node and npm.
2) capsule - https://github.com/andyet/capsule
I haven't tried this yet, but it seems similar. I'd be interested if anyone has interest with either of these projects.
I'm currently working on a framework named "onecode" that does what you asked for. Currently it lacks documentation, but I have a working project based on it, so it could work for you too. I'm also looking for contributors.
Here's how it works. Almost all code is shared between client and server, including models and views.
$.ajax
method).This means that:
The architecture forces some sane requirements which will make you a better developer. Something like:
A very simple example can be found in the source tree. I use Backbone as a basis for Models and Views, and Browserify deliver js package to client.
In my project I use inline templates with EJS as a templating engine. This has advantage of keeping HTML and code (I use CoffeeScript) in the same place. But the framework is capable of packaging templates from external files, with other templating engines like Jade. Please, see a templating example on how it could be done.
Please let me know if you are interested this approach, maybe it'd push me to start writing docs for it.
I haven't used it for node, but I've used mustache pretty extensively for backbone.js and been pleased with the results, and it has a port to use it with node as well.
http://mustache.github.com/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With