Search code examples
jqueryhtmljquery-templatesjsrender

Get array value within jsrender template


I have the JSONArray and pass this to jsrender template. How could I get array value within jstemplate.

Array Value

var clrarr = ["white", "black"]; var clrcode = ["#fff", "#000"];

Template

<select>
     {{for clrarr}}
           <option value="{{>#data}}">{{>clrcode[#index]}}</option>
     {{/for}}
</select>

How can I get the clrcode value within the jsrender.

Thanks in advance.


Solution

  • This is essentially a duplicate of other stackoverflow questions asking how to access "parent data" in JsRender. For example:

    Take a look at Accessing parent data

    In your case, you can write, for example:

    <select>
         {{for clrarr}}
               <option value="{{>#data}}">{{>~root.clrcode[#index]}}</option>
         {{/for}}
    </select>
    

    or

    <select>
         {{for clrarr ~clrcode=clrcode}}
               <option value="{{>#data}}">{{>~clrcode[#index]}}</option>
         {{/for}}
    </select>