theme_item_list生成的li标签添加不同的style class
theme_item_list生成的li标签是没有指定style class的,我们通过修改达到自己的需要,看下面为li标签增加<li class="odd">和<li class="even">
关于 function phptemplate_item_list2 函数请先看让theme_item_list生成的列表更易读
function phptemplate_item_list2($items = array(), $title = NULL, $type = 'ul', $attributes = NULL) {
$output = '<div class="item-list">';
if (isset($title)) {
$output .= '<h3>'. $title .'</h3>';
}
if (!empty($items)) {
$output .= "<$type" . drupal_attributes($attributes) . '>';
foreach ($items as $item) {
$attributes = array();
$children = array();
if (is_array($item)) {
foreach ($item as $key => $value) {
if ($key == 'data') {
$data = $value;
}
elseif ($key == 'children') {
$children = $value;
}
else {
$attributes[$key] = $value;
}
}
}
else {
$data = $item;
}
if (count($children) > 0) {
$data .= phptemplate_item_list2($children, NULL, $type, $attributes); // Render nested list
}
$output .= '<li' . drupal_attributes($attributes) . '>'. $data .'</li>';
}
$output .= "</$type>";
}
$output .= '</div>';
return $output;
}
$output = '<div class="item-list">';
if (isset($title)) {
$output .= '<h3>'. $title .'</h3>';
}
if (!empty($items)) {
$output .= "<$type" . drupal_attributes($attributes) . '>';
foreach ($items as $item) {
$attributes = array();
$children = array();
if (is_array($item)) {
foreach ($item as $key => $value) {
if ($key == 'data') {
$data = $value;
}
elseif ($key == 'children') {
$children = $value;
}
else {
$attributes[$key] = $value;
}
}
}
else {
$data = $item;
}
if (count($children) > 0) {
$data .= phptemplate_item_list2($children, NULL, $type, $attributes); // Render nested list
}
$output .= '<li' . drupal_attributes($attributes) . '>'. $data .'</li>';
}
$output .= "</$type>";
}
$output .= '</div>';
return $output;
}
修改为:
关于 function phptemplate_item_list2 函数请先看让theme_item_list生成的列表更易读
function phptemplate_item_list2($items = array(), $title = NULL, $type = 'ul', $attributes = NULL) {
$i = 1; //新增的
$output = '<div class="item-list">';
if (isset($title)) {
$output .= '<h3>'. $title .'</h3>';
}
if (!empty($items)) {
$output .= "<$type" . drupal_attributes($attributes) . '>';
foreach ($items as $item) {
$attributes = array();
$children = array();
if (is_array($item)) {
foreach ($item as $key => $value) {
if ($key == 'data') {
$data = $value;
}
elseif ($key == 'children') {
$children = $value;
}
else {
$attributes[$key] = $value;
}
}
}
else {
$data = $item;
}
if (count($children) > 0) {
$data .= phptemplate_item_list2($children, NULL, $type, $attributes); // Render nested list
}
$vars['stripe'] = $i++ % 2 ? 'even' : 'odd';//新增的
$output .= '<li' . drupal_attributes($attributes) . ' class="' . $vars['stripe'] . '">'. $data .'</li>';//新增的
//$output .= '<li' . drupal_attributes($attributes) . '>'. $data .'</li>';//注释掉的
}
$output .= "</$type>";
}
$output .= '</div>';
return $output;
}
$i = 1; //新增的
$output = '<div class="item-list">';
if (isset($title)) {
$output .= '<h3>'. $title .'</h3>';
}
if (!empty($items)) {
$output .= "<$type" . drupal_attributes($attributes) . '>';
foreach ($items as $item) {
$attributes = array();
$children = array();
if (is_array($item)) {
foreach ($item as $key => $value) {
if ($key == 'data') {
$data = $value;
}
elseif ($key == 'children') {
$children = $value;
}
else {
$attributes[$key] = $value;
}
}
}
else {
$data = $item;
}
if (count($children) > 0) {
$data .= phptemplate_item_list2($children, NULL, $type, $attributes); // Render nested list
}
$vars['stripe'] = $i++ % 2 ? 'even' : 'odd';//新增的
$output .= '<li' . drupal_attributes($attributes) . ' class="' . $vars['stripe'] . '">'. $data .'</li>';//新增的
//$output .= '<li' . drupal_attributes($attributes) . '>'. $data .'</li>';//注释掉的
}
$output .= "</$type>";
}
$output .= '</div>';
return $output;
}
也可以
if ($i++ % 2) {
$output .= '<li' . drupal_attributes($attributes) . ' class="odd">'. $data .'</li>';
}else {
$output .= '<li' . drupal_attributes($attributes) . ' class="even">'. $data .'</li>';
}
$output .= '<li' . drupal_attributes($attributes) . ' class="odd">'. $data .'</li>';
}else {
$output .= '<li' . drupal_attributes($attributes) . ' class="even">'. $data .'</li>';
}
Tag:
评论
发表新评论