How to use $attributes array?
supervova - March 26, 2008 - 23:14
Please, help
I’m unsuccessfully trying to design $items array for a long time. I’m designer and not familiar with php. So any assistance with theming it will be very much appreciated.
Challenge
I need to generate html output of this kind:
<ul class=”class1 class2”>
<li id=”id1”></li>
<li id=”id2”></li>
…
</ul>
There is themeable function theme_item_list which return a themed list of items. And it contain expressions
$output .= "<$type" . drupal_attributes($attributes) . '>';
and
$output .= '<li' . drupal_attributes($attributes) . '>'. $data .'</li>';
I guess it’s mean that we can add to list and list items some attributes with defined values. Am I right? And how to do it?
Thanks in advance

pass in an array, or add keyed elements with data
e.g.:
<?php
$items = array('big', 'bad', array('data' => 'wolf', 'class' => 'animal'));
$list_attributes = array('class' => 'fairytale');
print theme('item_list', $items, NULL, 'ul', $list_attributes);
?>
Thank you, Peter, very much
It's almost what I need
But maybe you can help me with using your solution to assign attributes to every items in foreach construct?
<?php$vid =1;
$items = array();
$terms = taxonomy_get_tree($vid);
foreach ( $terms as $term ) {
$count = db_result(db_query('SELECT COUNT(nid) FROM {term_node} WHERE tid = %d', $term->tid));
$items[] = l($term->name, "taxonomy/term/$term->tid") . " ($count)";
}
$list_attributes = array('class' => 'box');
if ( count($items) ) { print theme('item_list', $items, NULL, 'ul', $list_attributes);}
?>
Thanks again