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 hi
In terms of choosing a template language, you can pretty much take your pick with any js-based templating language, including Underscore Templates, Mustache or Handlebars - it's trivial to set up templates in a publicly accessible path that your Node.js application also reads when the content is generated server-side.
My personal favourite is Jade, which comes out of the box with Express - it enforces a very simplistic, expressive coding style.
You can find a good write-up of how to link together Backbone.js and Express here (using Jade as the template language).
Most of the examples tend to be how to set up a RESTful application which renders entirely client-side (and is not what you're really after).
However if you get your web app rendering in Express from templates in a publicly accessible folder (I would recommend keeping views separate - preprocess your templates and hand the html over to your views, so you keep the templates specific) then you'll be able to load them from Backbone.js too, and handle changes using Backbone.js's history stack.
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.
Try jsrender at https://github.com/BorisMoore/jsrender.
It is a rewrote from jQuery tmpl, and does NOT requires jQuery or any DOM. It is self-contained in a single file and the author keep it very fresh.
jsrender is a string-replacement engine for templating. We use it for both dynamic and static page in NodeJS. We also use it for non-HTML templates, e.g. XML, CSV, and text-based email. Although it is not in production mode yet, we have been using it for few months and the development is so far very stable.
Look at some cool demos at http://borismoore.github.com/jsrender/demos/demos.html.
At its simplest, you can do foreach loop. But if you are going expert route or be adventurous, you can have JS code running (either inlined, or passed as a function, a.k.a. helpers). Sure it is bad to have code inside presentation layer, but sometimes it don't hurt to have simple odd()/even() or nth+1 calculation in your favorite language. Just make sure you have a good understanding between layers and take the risks.