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 8848503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:26:17+00:00 2026-06-14T12:26:17+00:00

I am building my first web application with twitter bootstrap and I am liking

  • 0

I am building my first web application with twitter bootstrap and I am liking it, but I am running into a problem of repeating myself.

I am building a navigation bar, and I want the UI to reflect which tab is active based on the url. I am using backbone to route the urls. The only problem is that I have to manually code a bunch of different variables to be “active” or “” based on the url so that the tabs will reflect the appropriate state.

<template name="navbar">

<div class="container">
    <div class="row">
    <div class="span6">
    <ul class="nav nav-tabs">
        <li class="{{home}}">
            <a href="/home/">Home</a></li>

        <li class="{{1999s10}}{{1965malibu}}{{1960hearse}}{{1966gto}}{{1971blazer}} dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown">Cars<b class="caret"></b></a>

            <ul class="dropdown-menu">
                <li class="{{1999s10}}"><a href="/cars/1999s10">1999 Electric S-10</a></li>
                <li class="{{1965malibu}}"><a href="/cars/1965malibu">1965 Malibu</a></li>
                <li class="{{1960hearse}}"><a href="/cars/1960hearse">1960 Hearse</a></li>
                <li class="{{1966gto}}"><a href="/cars/1966gto">1966 GTO</a></li>
                <li class="{{1971blazer}}"><a href="/cars/1971blazer">1971 Bazer</a></li>
                <!-- <li class="divider"></li> -->
            </ul>
        </li>  
    </div>
    </div>
</div>

As you can see, right now, I have a bunch of variables for indicating what tabs are active. I have started to implement the following code, but Its so inelegant that I have stopped and want to use a better method.

Template.navbar.home = function () {
    return Session.equals("active", "home") ? "active" : '';
};

// a bunch more of these for every route

var TodosRouter = Backbone.Router.extend({
    routes: {
        "home/": "home",
            // more routes
    },
    home: function () {
        Session.set("active", "home");
    },
    // more functions for capturing the routes and setting the session variable "active"
});

Router = new TodosRouter;

Meteor.startup(function () {
    Backbone.history.start({pushState: true});
});

Using this method:
(1) has the issue of variables that start with numbers (my bad)
(2) requires me to route every url to change the Session “active” variable accordingly
(3) requires me to make a template handler for every button/url in the navigation bar

There has to be a better method for doing this, but I have yet to find it.

To reiterate, I want to find a way of accomplish activating the necessary tabs without breaking the DRY (do not repeat yourself) programming principle.

edit:

What would be really nice is something like this:

<li class="{{(url=="/cars/1999s10/")?"active":""}}"><a href="/cars/1999s10">1999 Electric S-10</a></li>

That would be a minimal amount of code.

  • 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-14T12:26:18+00:00Added an answer on June 14, 2026 at 12:26 pm

    I found out something that works:

    Handlebars.registerHelper('TabActive', function (route) {
        var url = Session.get("url");
        var index = $.inArray(route, url);
        return (index != -1) ? "active" : "";
    });
    
    
    var TodosRouter = Backbone.Router.extend({
        routes: {
            "*url": "main"
        },
        main: function (url) {
            Session.set("url", url.split('/'))
        }
    });
    

    and in the html, the ” vs ‘ is crucial!

    <li class='{{TabActive "home"}}'>
    

    this works, but my only concern is the using the router for other urls later…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building my first Facebook Connect application, and am running into an odd problem.
This is my first foray into building a secure web application. It will be
I'm building my first jquery mobile site and have run into a problem. When
I'm in the process of building my first web application using ASP.NET MVC 2
I am building my first .NET MVC web application and loving the simplicity and
I am building web application in Windows 7 OS running Apache Server, PHP 5.3
I am building my first ASP.Net web application project in VS2010. The site has
I am building a web application using VS 2012 MVC4 code first. In order
I'm building a web application and am looking into using Amazon S3 to store
I'm building my first web application after many years of desktop application development (I'm

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.