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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:34:02+00:00 2026-05-27T17:34:02+00:00

I am using underscore.js ‘s templating library, and I am not sure how to

  • 0

I am using underscore.js‘s templating library, and I am not sure how to go about using a logic inside of a template. for example, I would like to print an array of tags in a template. What is the best approach for this?

Javascript:

bunny_data = {
  name: "sprinkles",
  age: 1,
  tags: ['fuzzy','wuzzy']
};

bunny_view = $("#bunny-template").html();
$(body).append(_.template(bunny_view,bunny_data));

Template:

<script type='text/template'>
  <div> 
     <h5><% = name %></h5>
     <ul class='tag-list'>
         <!-- How do I print the tags here? -->
     </ul>
  </div>
</script>
  • 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-05-27T17:34:03+00:00Added an answer on May 27, 2026 at 5:34 pm

    You don’t need to encapsulate bunny_data as @Ken suggests, you were on the right track. Whether or not you want to call _. functions or just use plain Javascript constructs is up to you, but there’s no built-in flow constructs in Underscore templates, to do that you just embed code (you might want to look into eco or Mustache or something if you want that).

    My example looks like this (almost the same as you own):

    <script type='text/template' id="bunny-template">
      <div> 
         <h5><%= name %></h5>
         <ul>
           <% for(var tag in tags) { %>
               <li><%= tags[tag] %></li>
           <% } %>
         </ul>
      </div>
    </script>
    

    with the following Javascript:

    bunny_data = {
      name: "sprinkles",
      age: 1,
      tags: ['fuzzy','wuzzy']
    };
    
    bunny_view = $("#bunny-template").html();
    $(body).append(_.template(bunny_view, bunny_data));
    

    You can verify that it runs here.

    (On a personal preference note, I’m a heavy user of all of Underscore except the templates for this reason, I’m not comfortable with the amount of code you must put in them if you have a more complicated use case).

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

Sidebar

Related Questions

I'm using the underscore.js templating function and have done a template like this: <script
I am using the underscore.js template library with my backbone example. My template looks
I am using underscore.js's templating capabilities from backbone.js, I have the following template that
I am using Underscore library and using its template feature. But i am getting
Should client-side templates like the following (using underscore's templating engine): <p class=foo><%= bar %></p>
I'm using the Underscore template (which is based on John Resig's Microtemplate) and whenever
I'm using backbone's underscore templating engine with the mustache formatting patterns. I have already
I am using VS2008 and creating forms. By default, the underscore of the character
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Help me please solve my problem. I'm using underscore.js lib and I have a

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.