apelliciari
apelliciari

Reputation: 8501

Is having Django and Backbone.js double templates avoidable?

I have a complex not SPA (not single page application), a classic multi-page site, in which i'd like to use Backbone.js.

The server side of the app is Django powered.

My problem is: for SEO reasons I have to load every html content server-side, using django templates, but then to get Backbone's full power I need underscore's template (or handlebars) to refresh my backbone's views.

So, i have to write the same templates twice, with different tecnologies and hooks.

How can I avoid this?

Upvotes: 1

Views: 477

Answers (2)

Ravi Hamsa
Ravi Hamsa

Reputation: 4721

Basically you can do that, All you need to make sure is you don't call render on views first time.

say my page has this html

<ul class="my-list">
 <li><a href="#">do something</a></li>
 <li><a href="#">do something</a></li>
 <li><a href="#">do something</a></li>
</ul>

initially you define a view

var MyView =  Backbone.View.extend({
  el:'.my-list',
  render:function(){
    this.collection.each(this.addItem, this);
  },
  addItem:function(){
    //do adding logic here
  }
})

instantiate using, but don't call render

 var myView = new MyView({
    collection: myCollection //any collection or model you like here
 })

when ever you want to update the view, call myView.render then. By this you get the benefit of SEO and backbone both. I guess popular web applications like You Tube does follow similar approach (may not be using Backbone).

Upvotes: 1

Tim
Tim

Reputation: 91

Actually, you can write a single-page application and still support SEO. Backbone's router accomplishes this by creating a separate URL for each state of the application. Your links throughout the application will be crawled. Google does a good job of crawling SPA's these days. I believe your decision not to create your site as an SPA was influenced by stale opinion.

Upvotes: 1

Related Questions