handlebars.js - Handlebars: Compiling template with nested iterators -
i'm trying create glossary of terms using handlebars. each term contains array of related terms. if try use {{ #each }} style syntax error. if use {{ related }} converts each item in array string. need @ each element individually can wrap in span class style.
data:
[ { "name": "habitat", "description": "the place in nature distinct features (such short grass, wet ground, dry rocks, etc.) animal calls home because provides needs survive: food, water , shelter.", "related": ["range", "habitat fragmentation"] }, { "name": "habitat fragmentation", "description": "when animal’s homeland or range split smaller pieces. <br><br>for example, if place road in middle of large field, have fragmented field 2 pieces. if animal’s den on 1 side of road , food on other, animal must take risk of crossing road in order survive.", "related": ["habitat"] } ] template:
<script id="glossary-template" type="x-handlebars-template"> {{#each .}} <li class="term"> <h3>{{ name }} {{ acronym }}</h3> <p>{{{ description }}}</p> <p>related terms:{{ #related }}<span class="tag">{{this}}<span>{{ /related }} </li> {{/each}} </script>
i think issue here was using 2 unnamed iterators. passed data compiled template named parameter, , works now.
$('ul').append(template({"terms": data})); <script id="glossary-template" type="x-handlebars-template"> {{#terms}} <li class="term"> <h3>{{ name }} {{ acronym }}</h3> <p>{{{ description }}}</p> {{#related}}<span class="tag">{{this}}</span>{{/related}} </li> {{/terms}} </script>
Comments
Post a Comment