首页 > 解决方案 > Drupal 7:视图中的硬编码布局

问题描述

在我的一个 D7 项目中,我使用了几个 tpl 文件来制作视图模板。请参阅下面的一些 tpl 文件代码。但是,我的客户已经查看了代码并评论说:

视图中的硬编码布局。不是创建具有不同显示布局的块(相同的显示布局在其他地方相同),而是在 Views 结果上有一个布局代码,它取决于结果的键。

我无法理解他所说的要点。如果你愿意,我可以分享一些额外的编码/管理面板屏幕截图

以下是一些 tpl 文件:

1.

<div id="programmes" class="page-section">
    <div class="container">
      <h1 class="section-heading"><?php echo t('OUR');?> <?php echo count($rows); ?> <?php echo t('key Programmes');?></h1>
      <h3><?php echo t('The XXX operate');?> <?php echo count($rows); ?> <?php echo t('key programmes to ensure the greatest global impact:');?></h3>
      <div class="programblock" >
      <?php
       if($rows) {
       $count = 1;
       foreach($rows as $id => $row)
       {
            $title       = $view->render_field('title', $id);
            $description = $view->render_field('field_programme_description', $id);
            $image       = $view->render_field('field_programme_image', $id);
            $icon        = $view->render_field('field_programme_icon', $id);
            $color       = $view->render_field('field_programme_background_color', $id);
        ?>  
          <a href="#" class="programcard w-inline-block" style="background-color:<?php echo !empty($color) ? $color : '#FFFFFF' ?>"><img src="<?php echo $image; ?>" class="progimage">
              <div class="programtext">
                  <div class="w-clearfix">
                      <div class="progtabicon smallpti"><?php echo $icon; ?><br></div>
                  </div>
                  <div>
                      <h2 class="progtitle"><?php echo $title; ?></h2>
                      <div><?php echo $description; ?></div>
                      <div class="readmorecard"><?php echo t('Read More...'); ?></div>
                  </div>
              </div>
          </a>
       <?php
          $count++;
       }
       }
       ?>
      </div>
    </div>
  </div>

2.

<div id="s5" class="relevant-news">
    <div class="container w-clearfix">
        <h3 class="section-heading"><?php echo t('Related content'); ?></h3>

        <?php
        if ($rows) {
            foreach ($rows as $id => $row) {
                $image = $view->render_field('field_image', $id);
                $body = $view->render_field('body', $id);
                $title = $view->render_field('title', $id);

                $date = $view->render_field('field_news_publist_date', $id);
                $formatted_date = date('d F Y', strtotime($date));

                $programme = $view->render_field('field_news_programme', $id);
                $type = $view->render_field('field_tags', $id);
                ?>
                <div class="_4-block nomar4">
                    <a href="#" class="nl2 w-inline-block">
                        <div class="tag" data-ix="tagtext"><span class="cblu">&lt;/span><span class="uhcgreen"> </span><span class="jyel">&lt;/span><br></div><img src="<?php echo $image; ?>" alt="image" class="cardimage">
                        <div class="card-text">
                            <div class="cardtitle"><?php echo $title; ?></div>
                            <div class="tagtext alt"><?php echo $formatted_date; ?></div>
                            <div><?php echo $body; ?></div>
                            <div class="tagtext alt"><?php echo $programme; ?></div>
                        </div>
                        <div class="pagetype news">
                            <div><span class="fa">&lt;/span> <?php echo $type; ?></div>
                        </div>
                    </a>
                </div>
        <?php
            }
        }
?>


    </div>
    <div class="container"><a href="#" id="more" class="button reverse w-button"><span class="fa">&lt;/span> <?php echo t('View all News &amp; Media'); ?></a></div>
</div>

3.

<?php  global $base_url;?>
<?php
   if(!empty($rows)){
    $articles = array();
    $video    = array();
    $gallery  = array();
    $carousel = array();
    foreach($rows as $article){
        $nid = $article['nid'];
        $node = node_load(trim($nid));
        if($node->type == 'article'){
            $articles[] = $nid;
        }elseif($node->type == 'news_video'){
            $video[] = $nid;
        }elseif($node->type == 'news_gallery'){
           $gallery[] = $nid;
        }
        $slider = 0;
        if($node->type != 'article'){
            $carousel[] = $nid;
        }
    }
    $items = array();
    $count = 0;
    if(!empty($carousel)){
            $slider = 0;
            foreach($carousel as $nid){
                $items[$slider][] = $nid;
                $count++;

                if(($count % 5) == 0) {
                $slider++;
                }
            }
    }
?>


    <!--Show stiky images-->
    <div class="container w-clearfix">
      <?php
        $i = 0;
        for($i = 0; $i < count($articles); $i++){
        $nid                = (int) $articles[$i];
        $node               = node_load(trim($nid));
        $title              = $node->title;
        $image              = file_create_url($node->field_image[LANGUAGE_NONE][0]['uri']);
        $news_type_id       = $node->field_tags[LANGUAGE_NONE][0]['tid'];
        $news_type          = taxonomy_term_load($news_type_id);
        $programmes         = $node->field_news_programme[LANGUAGE_NONE];
        $type_name          = $news_type->name;
        $type_icon          = path_icon($news_type->field_news_icon[LANGUAGE_NONE][0]['icon']);
        $description        = !empty($node->field_short_description[LANGUAGE_NONE]) ? $node->field_short_description[LANGUAGE_NONE][0]['value'] : "";
        $date               = $node->field_news_publist_date[LANGUAGE_NONE][0]['value'];
        $program_name       = array();
        $program_short_name = array();
        $node_url           = drupal_get_path_alias('node/' . $nid);
      ?>
      <div class="_2block nomar special">
        <a href="<?php print $node_url;?>" class="nl2 bgimage w-inline-block" style="background-image:linear-gradient(180deg, rgba(21, 36, 49, 0), #152431), url('<?php echo $image;?>');background-repeat:no-repeat;background-size:cover;">
          <div class="pagetype">
            <div><span class="fa fa-<?php print $type_icon;?>"></span> <?php echo $type_name;?></div>
          </div>
          <div class="card-text"></div>
          <?php if(!empty($programmes)){ ?>
                <div class="tag">
                   <?php foreach($programmes as $key2 => $pro){
                       $programme            = taxonomy_term_load($pro['tid']);
                       $bundle               = $programme->field_icon_programme[LANGUAGE_NONE][0]['bundle'];
                       $icon                 = path_icon($programme->field_icon_programme[LANGUAGE_NONE][0]);
                       $program_name[]       = $programme->name;
                       $program_short_name[] = $programme->field_short_name[LANGUAGE_NONE][0]['value'];
                       ?>
                        <i class="<?php echo $icon;?>"></i>
                   <?php } ?>
                    <br>
                </div>
          <?php } ?>
          <div class="card-text forcedown">
                <div class="cardtitle bigct"><?php echo $title;?></div>
                <div class="tagtext alt"><?php echo date("d M Y", strtotime($date));?></div>
                <div><?php echo $description;?></div>
                <?php if(!empty($program_short_name)){?>
                <div class="tagtext"><?php echo implode($program_short_name, ', ');?></div>
                <?php }?>
          </div>
        </a>
      </div>
      <?php
            if($i == 1) {
                break;
            }
        }
      ?>
    </div>

标签: drupaldrupal-7

解决方案


首先,视图为创建的一个视图提供多个“显示”。它使您可以创建此视图的各种显示来处理和管理数据/显示。- 您可以创建一个像“页面”这样的显示,这样您的视图就会有自己的 url。- 您可以创建另一个显示,如“块”,因此它提供了一个可以添加到区域的块

视图创建小教程

第二件事是了解模板钩子覆盖可用,因此您可以更改 html 并显示视图:

如果我的视图被命名为“test”并且我的显示被命名为“block”,我可以用文件覆盖

views-view--test--block.tpl.php

主题功能如何工作

第三件事,模板不能包含函数调用,它会产生一些性能问题,这不是一个好习惯。只能$variablesechoor printed。你可以看一下views module,在“themes”文件夹下就明白了。

最后,如果您需要在渲染之前修改一些数据,您有几个可用的挂钩custom modulestemplate.php允许您修改它。

可用的钩子列表

希望它可以帮助你:)


推荐阅读