Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'loop.parity' for zebra row coloring #26

Closed
tuomassalo opened this issue Mar 18, 2010 · 13 comments
Closed

Add 'loop.parity' for zebra row coloring #26

tuomassalo opened this issue Mar 18, 2010 · 13 comments

Comments

@tuomassalo
Copy link

Hello,

it's very common that one wants to color even and odd rows of a list/table/etc. with alternative colors. That's why I'm suggesting a shortcut variable 'loop.parity' that yields 'even' or 'odd'. It could be used like this:

  <tr class="{{ loop.parity }}">...</tr>

Very readable, and it also makes the css code more readable.

Here's a patch:

*** Twig.orig/Node/For.php  Wed Mar 17 21:39:34 2010
--- Twig/Node/For.php   Thu Mar 18 02:17:57 2010
***************
*** 90,95 ****
--- 90,96 ----
          ->write("  'revindex'  => \$length,\n")
          ->write("  'first'     => true,\n")
          ->write("  'last'      => 1 === \$length,\n")
+         ->write("  'parity'    => 'odd',\n")
          ->write(");\n")
        ;
      }
***************
*** 120,125 ****
--- 121,127 ----
          ->write("--\$context['loop']['revindex'];\n")
          ->write("\$context['loop']['first'] = false;\n")
          ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")
+         ->write("\$context['loop']['parity'] = (\$context['loop']['parity']==='even'?'odd':'even');\n")
        ;
      }
@tuomassalo
Copy link
Author

Oops, found |even and |odd only now. :)

So, without the patch one could write something like:

  <tr class="{{ loop.index|even ? 'even' : 'odd' }}">

Not as nice as the suggestion, but quite ok still.

@lmammino
Copy link

cycle node support would be nice too.
With Django, whose twig is widely inspired on, you can do something like this:
{% for o in some_list %}

...

{% endfor %}

@lmammino
Copy link

@fabpot
Copy link
Contributor

fabpot commented Mar 28, 2010

added the cycle filter (closed by 075f541)

@tuomassalo
Copy link
Author

Great work! The cycle seems far better than the patch I suggested.

@Koc
Copy link
Contributor

Koc commented Apr 5, 2010

Fabien, can you make cycle as tag, not filter?

{% for item in items %}
  {{ item }} - {% cycle ['apple', 'orange', 'citrus'] %}
{% endfor %}

@fabpot
Copy link
Contributor

fabpot commented Apr 5, 2010

If I do that, on which variable should I cycle? A global variable? If so, then depending on what you have on the page before, it won't always start at the same element.

@Koc
Copy link
Contributor

Koc commented Apr 5, 2010

Static variable inside Twig_Node_Cycle?

$var = rand(1, 999999);
$compiler
  ->write("\$seq$var = twig_iterator_to_array(")
  ->subcompile($this->seq)
  ->raw(");\n");

Does cycle tag can get access to name of variable name "\$seq$var"?

@fabpot
Copy link
Contributor

fabpot commented Apr 5, 2010

That's not possible. But even if it was, it means that the cycle is not predictable.

@lmammino
Copy link

lmammino commented Apr 5, 2010

This new feature is really great and makes many common and troubleful tasks a piece of cake ;) Thanks a lot for adding it!

However with django you can do:
{% for o in some_list %}

...

{% endfor %}

it seems that django cycles on the current iteration index variable (I have no idea about how django cycle beahves with nested iterations)...

I don't know how django implemented it (and how twig architecture is similiar to django template system) but its approch sometimes could be more readable and easier to use.

@fabpot
Copy link
Contributor

fabpot commented Apr 6, 2010

In Django, there is no state for the cycle tag, which means that you cannot tell which will be the first value used in a loop. To me, this is not a good thing.

@fabpot
Copy link
Contributor

fabpot commented Apr 6, 2010

That said, that's quite easy to implement a cycle tag like the one in Django.

@lmammino
Copy link

lmammino commented Apr 6, 2010

In Django, there is no state for the cycle tag, which means that you cannot tell which will be the first value used in a loop. To me, this is not a good thing.

So, with django, if you do something like this:
{% for o in some_list %}

...

{% endfor %}
...
{% for p in some_list2 %}

...

{% endfor %}
are you saying that the first cycle value used in the second loop is not predictable?
If so, it's NOT really a good thing!

On the other way, using the current twig cycle implementation, i'd like to know what could be the best way to use cycle inside a "foreach" loop.

Maybe:
{% for row in rows %}
...
{% endfor %}

... a little bit tricky for designers

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants