Clean up theme_item_list() and make it a simpler theme function

zeta ζ - May 10, 2008 - 02:48
Project:Drupal
Version:7.x-dev
Component:theme system
Category:task
Priority:normal
Assigned:zeta ζ
Status:needs work
Description

Following on from http://drupal.org/node/98696, and as discussed with Bèr Kessels on IRC;-

This splits up the logic and themeing for theme_item_list() making the theme function much simpler, using a helper function for the complex bit, and creating a new theme function to theme the item itself.

Also fixes a couple of possible bugs. Initialising $data and extra \n before first <li> (maybe not needed, but ‘as advertised’).

AttachmentSize
theme_item_list.patch3.4 KB
Testbed results
theme_item_list.patchfailedFailed: Failed to apply patch. Detailed results

#1

zeta ζ - May 28, 2008 - 23:47
Status:active» needs review

Sorry, no-one is going to review this unless I mark it CNR :-)

#2

flobruit - June 2, 2008 - 22:46

Good idea. A couple suggestions though:

  • some of the string manipulations with $output are a little twisted and make the code hard to read.
  • Having both theme_list_item() and theme_item_list() is confusing. I'd rather have a weird name like theme_item_list_item() than be confused about which function I shoud use.
  • Shouldn't theme_list_item() also be declared in the theme registry?

#3

zeta ζ - June 3, 2008 - 02:26

Thanks for reviewing this.

  1. I assume you are referring to theme_item_list(). I’ll admit it’s not the standard $output .= '...';, but twisted? One line is out of order, but are you referring to this, or too many . ’s, or embedding $output in the RHS?
    I think it is good practice to add opening and closing tags on the same line, building outwards in a co-ordinated way. Would double quoted stings make it more readable?;-
    function theme_item_list($items = array(), $title = NULL, $type = 'ul', $attributes = NULL) {
      $attrs  = drupal_attributes($attributes);
      $items_ = _theme_item_list($items);
      $output = "<$type $attrs>$items_</$type>";
      if (isset($title)) {
        $output = "<h3>$title</h3>$output";
      }
      $output = "<div class=\"item-list\">$output</div>";
     
      return $output;
    }
    or a template file?
  2. Yes, I can see what you mean: Will use your suggestion.
  3. I’m very hazy on this: I can cope with modules, but, in core, what should the hook in hook_theme() be? (assuming use of hook_theme()). I can’t find any examples in core! Does it not work for you because of this?

#4

flobruit - June 3, 2008 - 09:10

Regarding point 1, I see what you're doing now: building the HTML from the inside rather than from the beginning. Even though it's not really standard, it does makes sense. Maybe a little comment would make it clearer.

For point 3, theme functions from .inc files (like theme_item_list()) are all defined in drupal_common_theme() inside of common.inc.

#5

Damien Tournoud - June 3, 2008 - 13:23

About #3.1: in that case single-quoting is better than double quoting in that case.

About the string order, it is more conventional in Drupal (and arguably easier to read), to build the output in the text flow order. Something like that will do:

<?php
function theme_item_list($items = array(), $title = NULL, $type = 'ul', $attributes = NULL) {
 
$output  = "";
 
$output .= '<div class="item-list">';
  if (isset(
$title)) {
   
$output .= "<h3>$title</h3>";
  }
 
$output .= "<$type" . drupal_attributes($attributes) . '>' . _theme_item_list($items) . "</$type>";
 
$output .= '</div>';
  return
$output;
}
?>

By the way: there was a bug in your patch: the <h3> should go inside the <div> but outside the <ul>/<ol>.

#6

zeta ζ - June 3, 2008 - 18:34
  1. Used (mainly) single-quoted strings and added a comment. Building from the inside makes it easier to modify: If you change a <div> the </div> is right there, just begging to be kept in sync. Each line stands up for itself (once I get it right :-( ). Thanks for the catch Damien, I had put this in #3-1. but didn’t make a new patch.
  2. Thanks: couldn’t find this at all. added entry for theme_item_list_item.
AttachmentSize
theme_item_list_.patch 4.94 KB

#7

Arancaytar - November 10, 2008 - 12:07
Status:needs review» needs work

Just as a question, is there some legacy reason why we call it an "item_list" rather than just a "list"? I can't think of any definition of a "list" that does not consist of "items". After all, we don't call it "theme_cell_table" either.

This is particularly weird with the name "theme_item_list_item".

Perhaps an opportunity to clean up the API a bit more?

Also, patch no longer applies.

 
 

Drupal is a registered trademark of Dries Buytaert.