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’).
| Attachment | Size |
|---|---|
| theme_item_list.patch | 3.4 KB |
| Testbed results | ||
|---|---|---|
| theme_item_list.patch | failed | Failed: Failed to apply patch. Detailed results |

#1
Sorry, no-one is going to review this unless I mark it CNR :-)
#2
Good idea. A couple suggestions though:
#3
Thanks for reviewing this.
$output .= '...';, but twisted? One line is out of order, but are you referring to this, or too many.’s, or embedding$outputin 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;
}
#4
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
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:
<?phpfunction 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
<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.#7
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.