2

I am wondering whether it is worth converting to a more generic templating engine such as mustache or handlebars instead of Django templates. The options I see for myself are:

  1. Stay with Django Templates (only server side)
  2. Convert completely to Mustache/Handlebars or such
  3. Use a hybrid approcah: Mustache/Handlebars as well as some usage of Django templates where applicable (ugliest option?)

Although conversion to Mustache/Handlebars like system buys me the flexibility of using the same templates on the client as well as server side, I do lose certain advantages provided by Django's templating system (such as Template Inheritance).

I know that there are fervent arguments for and against client side templating as well as server side templating. Some argue that rendering HTML on a busy server could make it slower while others argue that rendering HTML from JSON on the browser could make the client slower.

I am also aware of the arguments for using server side html rendering for non-javascript enabled devices, but most devices these days have javascript support.

Are there real and clear reasons for converting to Mustache/Handlebars etc from the Django templating system?

Thanks.

Charles
  • 50,943
  • 13
  • 104
  • 142
Sid
  • 7,511
  • 2
  • 28
  • 41

1 Answers1

3

Your question doesn't really have a concrete answer. In my opinion, the upsides for Mustache-style client-side templating far outweigh those in favor of server-side templating. Mustache-style client-side templates are portable, scalable, easily understood by other developers and work on all browsers (even IE6). If you plan for your web application to grow big, you'll be thanking yourself down the line when your page generation is offloaded to the users browser and not handled on the server-side.

The template inheritance offered by Django is overrrated--you can accomplish the same thing with Mustache templates and a little bit of JavaScript. Don't try to build large all encompassing templates. Build your templates as small re-usable building blocks and piece them together with JavaScript to form the components that comprise your web application.

Of course, SEO takes a hit when you render everything on the client, but that may or may not be an issue for your application.

Elliot B.
  • 17,060
  • 10
  • 80
  • 101
  • Thanks. After thinking more it seemed to me that it may make sense to use server side templates for static content while client templates for dynamic - such as updates on a page. For a static page there is in my case a very limited amount of HTML that would be sent from a server but for dynamic content there could be many instances of a widget (like a collection of comments to a blog) in which case only sending JSON from the server instead of HTML would definitely have bandwidth/efficiency advantages. Thoughts? – Sid Dec 28 '12 at 16:56
  • Here's an article I found very helpful: http://ryanflorence.com/2012/client-v-server-templating/ – JD Smith Nov 14 '13 at 07:51