-
Notifications
You must be signed in to change notification settings - Fork 863
Add Template Loops #29
Comments
I have to admit, I never really understood why the parameter order of the 'each' callback in jquery was 'index, value'. And now you're doing the same here. Seems like you need the index very rarely, yes? The django template loop is quite elegant. Within a loop, you have access to a special object 'loop'. That object has values like 'counter', 'first', 'last', etc. |
That'd be cute: <div data-loop="items">Position: <var>loop.index</var> Value: <var>loop.value</var></div> (And while we're at it, why is |
That's the idea, but I do still like being able to name the value of the iterated object. <div data-loop="items(vehicle)">You own a <var>vehicle</var>.</div> To help with nested loops, you could allow access to the loop object via a second name, "loop-LISTVAR". <var id="vehicles">[ "car", "boat" ]</var>
<var id="people">[ "Jane", "Rumplestilskin"]</var>
<div data-loop="people(person)">
<div data-loop="vehicles(vehicle)">
Person #<var>loop-people.index</var> (<var>person</var>): Vehicle #<var>loop-vehicles.index</var> (<var>vehicle</var>)
</div>
</div> |
I'm just unsure how much I like the Perhaps |
good point. Maybe even Or could use a second attribute if you specifically want it named (otherwise default to accessing via <div data-loop="items" data-loop-each="person"> |
An important use case could be generating random wrong choices, like you'd need to for a port of this code from solving_for_the_y-intercept: for (var i=0; i<5; i++)
{
var b_num = y1*slope_denominator-x1*slope_numerator;
var b_den = slope_denominator;
var bad_b_num = get_random();
var bad_b_den = get_random();
while (b_num*bad_b_den==b_den*bad_b_num || bad_b_den==0)
{
bad_b_den = get_random();
}
//possibleAnswers.push('m='+format_fraction(bad_m_num, bad_m_den));
Exercise.possibleAnswers.push('b='+format_fraction(bad_b_num, bad_b_den));
} |
Right, I already mentioned this to John (see #42). |
It would also be nice to have while loops and simple for loops (instead of iterator-based for each loops; I can't think of what the alternative is called). Maybe we could just read in data-loop and see what kind of loop it's specifying, like: <div data-loop="for (var i = 0; i < 10; i++)">
<div data-loop="for (person in people)">
<div data-loop="while (a !== b)"> This could, I guess be unified with |
I actually really like that. (Except |
yeah. I guess I also forgot about labeling the index, so maybe iterator-based loops could be something like <div data-loop="each (people, i, person)"> or <div data-loop="for ((i, person) in people)"> |
Yuck, now I don't really like it any more. It was nice before because it was the same as standard JS loop syntax. |
well, standard js loop syntax doesn't allow reference to the index, which we'd want somehow. I still think it's better than <div data-loop="people/i/person"> |
so, standard JS syntax, coupled with the aforementioned |
That is, assuming <div data-loop="for (person in people)">
Person #<var>loop.index</var>: <var>person</var>
</div> |
I don't like that solution that much. It looks elegant, but it doesn't generalize easily to nested loops. I was thinking something like this instead: <div data-loop="for (person in people)" data-loop-index="i"> |
It would be just as easy to specify the magic var: <div data-loop="for (person in people)" data-loop-var="loopPeople"> One reason why I harp on this is that it's very similar to the Django template loop tag. Which is what is being used on the backend. The |
I think I need this for issue #101 where I need a variable number of hints depending on the length of the polynomial. Thoughts? |
Presumably. I think that @jeresig is almost done with this. |
Perhaps something like this:
The text was updated successfully, but these errors were encountered: