继续Drupal分享精神 QQ群:107748121 站长QQ:2275288328 skype: hellodrupal【drupal交流+drupal 建站+theme制作】
登录 注册

theme_links Drupal 主题函数 API

今天看了一下这个函数,记录下来一些使用:theme_links 函数位于文件
includes/theme.inc, l第 542行
适合版本:
4.6 – 4.7
theme_links($links, $delimiter = ' | ')
在5 – 7版本使用:
theme_links($links, $attributes = array('class' => 'links'))

返回一组衔接,
参数:
$links :衔接数组: 衔接地址 和衔接标题

$attributes :衔接数组属性,如array('class' =>'links', 'id' => 'subnavlist')
返回值:

A string containing an unordered list of links.

代码:

<?php
function theme_links($links, $attributes = array('class' => 'links')) {
 
$output = '';

  if (
count($links) > 0) {
   
$output = '<ul'. drupal_attributes($attributes) .'>';

   
$num_links = count($links);
   
$i = 1;

    foreach (
$links as $key => $link) {
     
$class = $key;

     
// Automatically add a class to each link and also to each LI
     
if (isset($link['attributes']) && isset($link['attributes']['class'])) {
       
$link['attributes']['class'] .= ' ' . $key;
      }
      else {
       
$link['attributes']['class'] = $key;
      }

     
// Add first and last classes to the list of links to help out themers.
     
$extra_class = '';
      if (
$i == 1) {
       
$extra_class .= 'first ';
      }
      if (
$i == $num_links) {
       
$extra_class .= 'last ';
      }
     
$output .= '<li '. drupal_attributes(array('class' => $extra_class . $class)) .'>';

     
// Is the title HTML?
     
$html = isset($link['html']) && $link['html'];

     
// Initialize fragment and query variables.
     
$link['query'] = isset($link['query']) ? $link['query'] : NULL;
     
$link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;

      if (isset(
$link['href'])) {
       
$output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
      }
      else if (
$link['title']) {
       
//Some links are actually not links, but we wrap these in <span> for adding title and class attributes
       
if (!$html) {
         
$link['title'] = check_plain($link['title']);
        }
       
$output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
      }

     
$i++;
     
$output .= "</li>\n";
    }

   
$output .= '</ul>';
  }

  return
$output;
}
?>

实例:

<?php
$ls
[] = array('href' =>'http://www.hellodrupal.info','title' => '你好drupal'); //定义一个links数组
print theme('links', $ls, array('class' => 'lss'));

?>

测试输出结果:
<ul class="lss">
   <li class="0 first last">
   <a href="htttp://www.hellodrupal.info">你好drupal</a>
   </li>
</ul>   

在主题garland 主题page.tpl.php模板文件里面使用了此函数:
        <?php if (isset($primary_links)) : ?>
          <?php print theme('links', $primary_links, array('class' => 'links primary-links')) // 可以扩展如:array('class' =>lprimary_links, 'id' => 'subnavlist')?>
        <?php endif; ?>
        <?php if (isset($secondary_links)) : ?>
          <?php print theme('links', $secondary_links, array('class' => 'links secondary-links')) ?>
        <?php endif; ?>

输出结果:
<ul class="links primary-links"><li class="menu-144 active-trail first active"><a href="/taxonomy/term/4" title="" class="active">Drupal 主题</a></li>
<li class="menu-141"><a href="/taxonomy/term/3" title="">Drupal 使用</a></li>
<li class="menu-143"><a href="/taxonomy/term/2" title="">Drupal 开发</a></li>
<li class="menu-142"><a href="/taxonomy/term/1" title="">Drupal 模块</a></li>
<li class="menu-181 last"><a href="/forum" title="">Drupal 知道</a></li>
</ul>                                 
      </div>

评论

发表新评论

此内容将保密,不会被其他人看见。
  • 自动将网址与电子邮件地址转变为链接。
  • 自动断行和分段。
  • You can use BBCode tags in the text.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

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

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.