所以我正在尝试使用 Mustache 和 Sinatra 从数组中呈现选择下拉菜单。模板代码目前如下所示:
<select id="phone_prefix" name="phone_prefix">
{{#prefixes}}
<option value="{{to_s}}" {{selected}}>{{to_s}}</option>
{{/prefixes}}
</select>
使用视图中的以下方法,它正在渲染数组的每个项目:
def prefixes
["03", "04", "06", "07", "09", "021", "022", "025", "027", "028", "029"]
end
对于{{selected}}
mustache 模板中的值,我需要对当前正在迭代的数组项和传入的查询字符串值进行比较params[:phone_prefix]
,例如 is "09"
。然后,当有匹配时,将值返回给传递给 mustache"selected"
的方法。selected
任何帮助将不胜感激。