Skip to content

Instantly share code, notes, and snippets.

@robmint
Created February 21, 2016 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robmint/fb074b1da2f1c1c46c9d to your computer and use it in GitHub Desktop.
Save robmint/fb074b1da2f1c1c46c9d to your computer and use it in GitHub Desktop.
Unique keys in data structures with handlebars templating
// typical input data
// Years and semesters need to be unique so they are stored as keys
var data = {
count: 2,
found: 5,
results: {
'2011': {'Semester 1': {url: 'http://site.nz/?pid=401067'}},
'2012': {
'Semester 1': {url: 'http://site.nz/?pid=493997'},
'Semester 2': {url: 'http://site.nz/?pid=493945'}
}
},
};
// handlebars template would go like this
/*
<ul>
{{#each data.results}}
<li>{{@key}}
{{#each this}}
<a href="{{url}}">{{@key}}</a>
{{/each}}
</li>
{{/each}}
</ul>
*/
// html output
/*
<ul>
<li>2011
<a href="http://site.nz/?pid=401067">Semester 1</a>
</li>
<li>2012
<a href="http://site.nz/?pid=493997">Semester 1</a>
<a href="http://site.nz/?pid=493945">Semester 2</a>
</li>
</ul>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment