Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8191485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:55:51+00:00 2026-06-07T03:55:51+00:00

I have 2 different templates for my model’s views. Each time the models are

  • 0

I have 2 different templates for my model’s views. Each time the models are fetched from the database, the first 3 models (#1, 2, 3) fetched from the backend will have the view created using the first template, the next 4 models (#4, 5, 6, 7) will use the second template, the next 3 models (#8, 9, 10) will use the first template and so on.

Problem: How will I introduce this alternating template using backbone.js?

JS Code

// Views

PhotoListView = Backbone.View.extend({
    el: '#photo_list',

    render: function() {
        $(this.el).html('');
        _.each(this.model.models, function(photo) {
            $(this.el).append(new PhotoListItemView({ model: photo }).render().el);
        }, this);
        return this;
    }
});

PhotoListItemView = Backbone.View.extend({
    tagNAme: 'div',
    className: 'photo_box',

    template: _.template( $('#tpl_PhotoListView').html() ),

    initialize: function() {
        this.model.bind('destroy', this.close, this);
    },

    render: function() {
        $(this.el).html( this.template( this.model.toJSON() ) );
        return this;
    },

    close: function() {
        this.unbind();
        this.remove();
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:55:52+00:00Added an answer on June 7, 2026 at 3:55 am

    First of all, your PhotoListView is wrapping a collection so you should be using this.collection inside the view and new PhotoListView({ collection: c }) to create it. Views treat the collection option similarly to how they treat model:

    constructor / initialize new View([options])

    […] There are several special options that, if passed, will be attached directly to the view: model, collection, el, id, className, tagName and attributes.

    Using the right names will help prevent confusion. Also, views have some Underscore methods already mixed in so you can say this.collection.each(...) instead of _.each(this.collection.models, ...) or _(this.collection.models).each(...). You can also use this.$el instead of $(this.el).

    And now on to your real problem. You can add two templates to your per-model view:

    PhotoListItemView = Backbone.View.extend({
        template0: _.template($('#the-first-template-id').html()),
        template1: _.template($('#the-other-template-id').html()),
        //...
    });
    

    and an option to tell it which one to use:

    initialize: function(options) {
        this.template = this['template' + options.template_number];
        //...
    }
    

    Then you just need to specify the group option from the collection view. Underscore’s each passes the iteration index to the callback function as the second argument so you just need a bit of integer math to figure out which template_number to use:

    this.collection.each(function(photo, i) {
        // Grouped in threes and you're alternating between two templates.
        var n = Math.floor(i / 3) % 2; 
        var v = new PhotoListItemView({ model: photo, template_number: n });
        this.$el.append(v.render().el);
    }, this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have different php output in jQuery-based tabs. This output is formed from database
I have many different models and each does something different. In template i want
I have a tab control with two different potential item templates: <TabControl ItemTemplateSelector={StaticResource tabTemplateSelector}/>
I have different blocks of 34 threads each (0...33). I need to find a
I have to display different views for mobile devices and I want to provide
I have a model called Subtopic. One of my templates runs a forloop on
So in my Django project I have a few different apps, each with their
I have two sites running off the same base code. Each site has different
I want to use the same template to display records from different models in
I have a page with two different View Models: <?page title=My page contentType=text/html;charset=UTF-8?> <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.