Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Add Template Loops #29

Closed
jeresig opened this issue May 23, 2011 · 18 comments
Closed

Add Template Loops #29

jeresig opened this issue May 23, 2011 · 18 comments
Assignees

Comments

@jeresig
Copy link
Member

jeresig commented May 23, 2011

Perhaps something like this:

<var id="items">[ "a", "b", "c" ]</var>
// later...
<div data-loop="items(i,value)">Position: <var>i</var> Value: <var>value</var></div>
@ghost ghost assigned jeresig May 23, 2011
@pappy74
Copy link
Contributor

pappy74 commented May 25, 2011

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.

@sophiebits
Copy link
Contributor

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 jQuery.map backwards from the other iterators?)

@pappy74
Copy link
Contributor

pappy74 commented May 25, 2011

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>

@sophiebits
Copy link
Contributor

I'm just unsure how much I like the collection(item) style with parentheses because it looks a lot like a method call but isn't one.

Perhaps data-loop="people/person"? That feels marginally better to me.

@pappy74
Copy link
Contributor

pappy74 commented May 25, 2011

good point. Maybe even data-loop="person in people"

Or could use a second attribute if you specifically want it named (otherwise default to accessing via loop.value):

<div data-loop="items" data-loop-each="person">

@osnr
Copy link
Contributor

osnr commented May 27, 2011

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));
}

@sophiebits
Copy link
Contributor

Right, I already mentioned this to John (see #42).

@jeffinitelyjeff
Copy link
Contributor

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 data-if to create some kind of generic data-control which we check to see if it's specifying an if, elseif, loop, etc. Not sure if that'd be a desired direction, though.

@sophiebits
Copy link
Contributor

I actually really like that. (Except int should be var and also for/in is weird sometimes.)

@jeffinitelyjeff
Copy link
Contributor

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)">

@sophiebits
Copy link
Contributor

Yuck, now I don't really like it any more. It was nice before because it was the same as standard JS loop syntax.

@jeffinitelyjeff
Copy link
Contributor

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">

@pappy74
Copy link
Contributor

pappy74 commented Jun 2, 2011

so, standard JS syntax, coupled with the aforementioned loop variable?

@pappy74
Copy link
Contributor

pappy74 commented Jun 2, 2011

That is, assuming people is an array:

<div data-loop="for (person in people)">
   Person #<var>loop.index</var>: <var>person</var>
</div>

@jeffinitelyjeff
Copy link
Contributor

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">

@pappy74
Copy link
Contributor

pappy74 commented Jun 2, 2011

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 loop variable can also have other useful properties: see https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#for for examples.

@robertyates
Copy link
Contributor

I think I need this for issue #101 where I need a variable number of hints depending on the length of the polynomial. Thoughts?

@sophiebits
Copy link
Contributor

Presumably. I think that @jeresig is almost done with this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants