Try to make it a bit more complex:
<div if="orders.Any()" each="var order in orders">
Here's your order #${orderIndex+1}:
<ul>
<li each="var p in order.Products">
${pIndex}: ${p.Name}
<span if="pIsLast"> (the end)</span>
</li>
</ul>
</div>
I can see the flow here. I can actually see HTML here. Now take a look:
<% if (orders.Any()) { %>
<% var orderIndex = 0; foreach (var order in orders") { %>
<div>
Here's your order #<%= (orderIndex+1) %>
<ul>
<% int pIndex = 0; foreach (var p in order.Products)
{ bool pIsLast = pIndex == products.Count; %>
<li>
<%= pIndex %>: <%= p.Name %>
<% if (pIsLast) { %>
<span> (the end)</span>
<% } %>
</li>
<% ++ pIndex; } %>
</ul>
</div>
<% orderIndex++; } %>
<% } %>
I'm lost here. Is there any HTML there?
For me, this is the main reason. Of course, Spark gives TONS of features - macros (where you code your Html.Helpers in spark markup), PDF export, etc - listed by others - but as a programmer I prefer clean code.
As another example, do you often use for (int i = 0; i < products.Count; i++) { Product product = products[i]; .... } these days? Would you prefer foreach (var product in products) {}? I would. Not only because it's less to type. Because:
- it expresses the intent better
- it reads better
- it hides additional details (like .Count, .Length, or .Count(); or if it's IEnumerable that you have to traverse in a special way) from my tired mind
- it reduces number of variables cluttering the context (and my tired mind)
- thus I can concentrate on the problem, nothing goes in the way
- it helps to avoid {} since there's no need to have a variable inside - reducing line count
- and when you change the collection from IList to array, you don't change 50 loops all over the place
and that's about such simple thing as foreach. Each reason is simple, but they sum up to something bigger. And these reasons apply perfectly to Spark. Hey, looks like I'm in love ;-)
Update: now, you know what? Look at my post's edit history. I had to edit the damn ASP code several times just because I missed some bits here and there. I just do not see if it's right or wrong. It's completely hidden if I have the span there, or condition at place.
Update: hm, yet another edit... moving ASP's if/span inside li...