Item Lists using even/odd rows

NancyDru - November 16, 2007 - 22:02
Project:Drupal
Version:7.x-dev
Component:theme system
Category:feature request
Priority:normal
Assigned:NancyDru
Status:patch (code needs review)
Description

I realize it makes the code more complex, but it would be nice if we had the option of using the even and odd class separation on item lists (theme_item_list) like we can get on tables.

#1

NancyDru - February 1, 2008 - 16:49
Priority:minor» normal

#2

NancyDru - February 1, 2008 - 19:42
Assigned to:Anonymous» NancyDru
Status:active» patch (code needs review)

Patch based on 6-rc3.

AttachmentSize
item_list.patch728 bytes

#3

dvessel - February 1, 2008 - 21:52
Status:patch (code needs review)» patch (code needs work)

This isn't testing for the right condition.

if ($i & 1) {
...

How about doing what theme_table does by setting up a flip array and letting it toggle back and forth. Nice and efficient.

#4

NancyDru - February 1, 2008 - 22:21

The flip technique is interesting; I've never seen that one before. When did theme_table add that instead of "$count%2"?

It looks to me like $i is the "row" number of the list, so it works. Note that is not just a test - it is also an AND that looks at the low-order bit of the number. I think that's faster than the "%2" technique. Certainly my limited testing came out correctly, although I was a bit surprised because I always think of the first row as being 1 (odd) instead of 0 (even).

#5

dvessel - February 1, 2008 - 22:37
Status:patch (code needs work)» patch (code needs review)

Ah! I was thinking "&&". Your using a bitwise operator so it does work. I just never seen that done in core and that's where I learned php so, my mistake. :)

This being new to me, I'll let someone else judge on the coding style.

#6

NancyDru - February 2, 2008 - 01:20

Yes, the first time I saw that, I thought the same thing and had to go look at the manuals to realize what it was doing. I figured that it was faster, so I just adopted the practice. I do like that "flip" technique too. I wonder which is the fastest?

#7

zeta ζ - May 10, 2008 - 03:43

I prefer the ($i & 1) technique: It obviously isn’t familiar to everyone, but it is all there in one place, whereas the flip technique looks very odd until you realise that; yes, it is supposed to be using the value from the previous iteration; yes, it is initialised properly; oh, that’s what the weird array that relies on string matching is for.

$count % 2 could possibly be optimised if it didn’t mess about with negative values, so I think ($i & 1) is the most efficient.

I’ve just issued a patch for theme_item_list(), so if that gets in, and before this one, I just wanted to flag before i forget, that the code can be (line: 1459);-

<?php

   
if ($i & 1) {
     
$attributes['class'] = empty($attributes['class']) ? 'odd' : ($attributes['class'] .' odd');
    }
    else {
     
$attributes['class'] = empty($attributes['class']) ? 'even' : ($attributes['class'] .' even');
    }
    if (
$i == 0) {
     
$attributes['class'] .= ' first';
    }
    if (
$i == $num_items - 1) {
     
$attributes['class'] .= ' last';
    }
?>

My IDE wont let me make a patch of this change until one is committed :-(

#8

chx - May 10, 2008 - 10:20
Status:patch (code needs review)» patch (code needs work)

I am someone who to this day still divides two by shifting one to right, so believe, masking by one to test for even/odd is familiar to me. Still the class is more a PHP construct and we are coding in PHP, not C or assembly.

#9

NancyDru - May 10, 2008 - 12:54

Well, as an old assembler programmer, I try to code for speed as well as readability (that's why we have comments). I haven't benchmarked it, but I have to believe $i & 1 is considerably faster than $i % 2 and the flip technique. When I think about all the lists that show up on my sites, this might be enough savings to make shared hosting (by far the majority) just a tad better.

#10

zeta ζ - July 8, 2008 - 02:57

I know this still has an &, but is this a bit clearer? & is part of php — being part of C doesn’t mean we can’t use it if it is the most appropriate construct.

(Hope this applies against HEAD: I’ve got several uncommitted patches on theme.inc — it might need an offset)

AttachmentSize
item_list_2.patch1.11 KB

#11

zeta ζ - July 8, 2008 - 02:58
Status:patch (code needs work)» patch (code needs review)

#12

swentel - August 13, 2008 - 22:30
Status:patch (code needs review)» patch (code needs work)

Cool idea, patch didn't seem to apply to head anymore, rerolled patch.

AttachmentSize
item_list.patch900 bytes

#13

zeta ζ - August 13, 2008 - 23:08
Status:patch (code needs work)» patch (code needs review)

Thanks for the re-roll.

You don’t say what work is needed (apart from the re-roll (which you’ve done))

Have you reviewed it? ;-)

#14

swentel - August 13, 2008 - 23:23
Status:patch (code needs review)» patch (reviewed & tested by the community)

Ow sorry, somehow slipped that select box heh :)
Did a quick review, added following php code in a page with php filter:

<?php
$options
= array('one (should have first)', 'two', 'three', 'four (should have last)');
$result = theme('item_list', $options);
echo
$result;
?>

and got this
<ul>
<li class="even first">one (should have first)</li>
<li class="odd">two</li>
<li class="even">three</li>
<li class="odd last">four (should have last)</li>
</ul>

which is good :) Setting to rtbc!

#15

swentel - August 13, 2008 - 23:34
Status:patch (reviewed & tested by the community)» patch (code needs work)

Ok, I was a bit to fast settting it to RTBC. When looking at other listings in drupal core (admin/content/node or watchdog), the first row always starts with 'odd'. Maybe we should follow this logic also here.

#16

swentel - August 13, 2008 - 23:40
Status:patch (code needs work)» patch (reviewed & tested by the community)

Ok, switch odd and even, output is now

<ul>
<li class="odd first">one (should have first)</li>
<li class="even">two</li>
<li class="odd">three</li>
<li class="even last">four (should have last)</li>
</ul>

AttachmentSize
list.patch901 bytes

#17

Dries - August 14, 2008 - 17:43

Patch looks good. It is not entirely clear how commonly used this would be though. I guess there might be a rare use case for this.

#18

NancyDru - August 14, 2008 - 18:12

I personally would use it a lot. I suspect you'll see more and more uses for it, just like in tables.

#19

Dries - August 16, 2008 - 21:19
Status:patch (reviewed & tested by the community)» patch (code needs review)

One more thought. We might be able to clean-up the pager.inc code a bit now? I believe it is adding first-page CSS classes which might now be redundant? Let's try to answer this question before we proceed. Thanks!

#20

swentel - August 17, 2008 - 14:06

Hmm, I fail to see right now how this is related to pager. Pager uses it's own theme functions and pagers aren't in a nested list at this point.
(there is some debate on another issue about pager though at #294449, maybe take it to there ?)

 
 

Drupal is a registered trademark of Dries Buytaert.