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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:22:38+00:00 2026-05-27T16:22:38+00:00

This is what I’d like to do in Mustache.js but not seeing how with

  • 0

This is what I’d like to do in Mustache.js but not seeing how with the documentation.

var view = {items:['Mercury','Venus','Earth','Mars']};
var template = "<ul> {{#items}}<li>{{i}} - {{.}}</li>{{/items}} </ul>";
var html = Mustache.to_html(template,view);

Desired output:

<ul>
  <li>0 - Mercury</li>
  <li>1 - Venus</li>
  <li>2 - Earth</li>
  <li>3 - Mars</li>
</ul>
  • 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-27T16:22:38+00:00Added an answer on May 27, 2026 at 4:22 pm

    An alternative solution, without fooling around with Mustache.js

    Instead of fooling around with mustache you might as well use a <ol></ol> instead of <ul></ul>, that will prefix each item with index+1.

    If you’d like you can use css to change the starting number to 0, and it will render exactly as you want. You can even change the dot after the number, to something such as " - ", and problem is solved.

    <ol>
      <li>Mercury</li>
      <li>Venus</li>
      <li>Earth</li>
      <li>Mars</li>
    </ol>
    

    the above will render as:

    1. Mercury
    2. Venus
    3. Earth
    4. Mars
    

    the Mustache.js way to do it

    The proper method if you’d like to do it in mustache is to convert your array of strings to an array of objects, where index and string value is present.

    // I wrote this from the back of my head, it's untested and not guaranteed
    // to work without modifications, though the theory behind it is valid.
    
    
    var array     = ["123","stackoverflow","abc"];
    var obj_array = [];
    
    for (idx in array)
       obj_array.push ({'index': idx, 'str': array[idx]});
    
    var view     = {items: obj_array};
    var template = "<ul>{{#items}}<li>{{index}} - {{str}}</li>{{/items}}</ul>";
    var html     = Mustache.to_html(template,view);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This might seem like a stupid question I admit. But I'm in a small
this is most likely very easy but i'm struggling $(window).bind(load resize, function(){ var wH
For some reason, after submitting a string like this Jack’s Spindle from a text
This might be a silly question but it just popped up in my mind.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This question was posted on StackApps , but the issue may be more a
This seems 101-level, but I can't find an answer! (instead I find links to
This works fine, but I want to make it prettier - and accommodate all
This is a pretty simple question, and I searched the previous questions but couldn't

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.