Created
February 21, 2016 22:18
-
-
Save robmint/fb074b1da2f1c1c46c9d to your computer and use it in GitHub Desktop.
Unique keys in data structures with handlebars templating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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