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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:40:26+00:00 2026-05-25T17:40:26+00:00

So I am trying to render a select drop-down from an array using Mustache

  • 0

So I am trying to render a select drop-down from an array using Mustache and Sinatra. The template code currently looks like this:

<select id="phone_prefix" name="phone_prefix">
    {{#prefixes}}
        <option value="{{to_s}}" {{selected}}>{{to_s}}</option>
    {{/prefixes}}
</select>

With the following method in the view it is rendering each item of the array:

def prefixes
  ["03", "04", "06", "07", "09", "021", "022", "025", "027", "028", "029"]
end

For the {{selected}} value in the mustache template I need to do a comparison on the array item currently being iterated over and a query string value coming in via params[:phone_prefix] which for instance is "09". Then when there is a match return a value of "selected" to a selected method to pass to mustache.

Any help would be greatly appreciated.

  • 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-25T17:40:26+00:00Added an answer on May 25, 2026 at 5:40 pm

    Push the logic into your Ruby and just feed data to Mustache. Update your prefixes method to return an Array of Hashes:

    def prefixes
      # figure out which one is selected by asking params...
      [ { :n => '03', :selected => false }, { :n => '04', :selected => true }, ... ]
    end
    

    and then something like this in your template:

    <select id="phone_prefix" name="phone_prefix">
        {{#prefixes}}
            <option value="{{n}}" {{#selected}}selected="selected"{{/selected}}>{{n}}</option>
        {{/prefixes}}
    </select>
    

    I’ve only used Mustache in JavaScript so I might be missing something in the Ruby flavor but something close to that should work, you just have to get information from params into your prefixes method.

    Mustache is intentionally minimalistic and free of logic, it even says so on the homepage:

    Logic-less templates.

    So all your logic goes in your code and you just set a bunch of variables and flags for Mustache to do things with. The only logic available in the template is “is this thing true?” and “can I iterate over this thing?”; anything more complicated than that has to go in the code that is preparing data for Mustache.

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

Sidebar

Related Questions

Im trying to render a django template from a database outside of djangos normal
I'm using the ui-multiselect widget to render a <select> list differently. I'm trying to
I am trying to render a model in Direct3D using DrawIndexedPrimitives . However, I
I'm trying to render a bit of text using Core Graphics APIs and I'm
I am trying to render a complex XML document as webpage(FF only) using a
Very simple problem...but weird results. Im just trying to fill a drop down list
I'm trying to render sliders instead of select components. Each page has several select
I am trying to filter Client Comments by using a select and rendering it
I'm trying to insert a drop down menu values to a table named units
I am trying to create an extension that renders a read-only drop down list.

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.