douya2982 2014-03-21 05:19
浏览 42
已采纳

drupal 7 template.php中classes数组的用途是什么

in template.php many times they used classes_array..am not getting the meaning and why they using,..what is the purpose of classes_array and when we have to use that in drupal7 .tpl.php

    example code:
    if(in_array('administrator',array_values($variables['user']->roles)))
    {
     $variables['classes_array'][]="debug";
      }
  • 写回答

1条回答 默认 最新

  • dqo58772 2014-03-23 15:39
    关注

    $variables['classes_array'] is used in preprocess functions. It adds classes to be used in rendering the element to be processed. In your example, a class named "debug" will be added to the html container of the rendered element: if the actual code is

    function <YOUR THEME>_preprocess_html(&$variables) {
      if (in_array('administrator',array_values($variables['user']->roles))) {
        $variables['classes_array'][]="debug";
      }
    }
    

    your theme will output a body tag like

    <body class='debug [...OTHER CLASSES...]'>
    

    for users with administrator role.

    You can also add classes to nodes, or other kind of elements for which a preprocess hook is available. E.g. you could write a node preprocess function:

    function <YOUR THEME>_preprocess_node($variables) {
      $classes_array[] = 'my-class';
    }
    

    if you wanted to add 'my-class' to every node of your site.

    In general, you will not find $classes_array among the defined variables in tpl.php files. Your theme will, most of the times, implode them in a $classes variable. It must be noted, however, that a kind of confusion arised over time, so different themes may use $classes_array, $attribute_array, $classes, $attributes['class'] and so on for the same purpose, so you should check your theme's documentation to find out what suits your case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部