I'm working on my first site using client-side templating, and so far so good. I'm just looking to achieve the best performance for the code below.
I'm given json in this format:
[
{
"slidenumber": "slide0",
"title": "Sample Data 1",
"slideDelay": "2000",
"months": [
{
"Jan": "10.3",
"Dec": "65",
"Nov": "87",
"Oct": "80",
"Sep": "70",
"Aug": "100"
}
]
},
{
"slidenumber": "slide1",
"title": "Sample Data 2",
"slideDelay": "2000",
"months": [
{
"Jul": "10",
"Jun": "20",
"May": "30",
"Apr": "40",
"Mar": "50",
"Feb": "60"
}
]
}
]
and using the underscore.js template below:
_.each(slides, function(slide){ %>
<li data-slide-delay="<%= slide.slideDelay %>">
<h1><%= slide.title %></h1>
<table class="table">
<thead>
<tr>
<%
_.each( slide.months, function(month) {
if (_.isObject(month)) {
_.each(month, function(value, key) {%>
<th><%= key %></th>
<%})
}
});%>
</tr>
</thead>
<tbody>
<tr>
<%
_.each( slide.months, function(month) {
if (_.isObject(month)) {
_.each(month, function(value, key) {%>
<td><%= value %></td>
<%})
}
});%>
</tr>
</tbody>
</table>
</li>
Basically, I'm building a slideshow using the data above, so each "slide" has specific fields title, delay, etc., months on the other hand has data inside of it.
I'm building an <li>
for each slide and within in that a horizontal <table>
, in that table I'm building <th>
for the month (i.e. Jan, Feb, Mar, etc.) and then I'm separating the month value in <td>
's
Rather than do two _.(each
statements with the same data (but echoing different values) as I'm doing above, what's the best method to achieve the desired output?