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;
}

修改为:
关于 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;
}

也可以

          if ($i++ % 2) {
      $output .= '<li' . drupal_attributes($attributes) . ' class="odd">'. $data .'</li>';
          }else {
      $output .= '<li' . drupal_attributes($attributes) . ' class="even">'. $data .'</li>';
           }

评论

发表新评论

此内容将保密,不会被其他人看见。
  • 自动将网址与电子邮件地址转变为链接。
  • 允许HTML标签:<a> <img> <em> <del> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h5> <h6>
  • 自动断行和分段。
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

更多关於格式化选项的信息

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.