drupal - 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"></span><span class="uhcgreen"> </span><span class="jyel"></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"></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"></span> <?php echo t('View all News & 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>
解决方案
首先,视图为创建的一个视图提供多个“显示”。它使您可以创建此视图的各种显示来处理和管理数据/显示。- 您可以创建一个像“页面”这样的显示,这样您的视图就会有自己的 url。- 您可以创建另一个显示,如“块”,因此它提供了一个可以添加到区域的块
第二件事是了解模板钩子覆盖可用,因此您可以更改 html 并显示视图:
如果我的视图被命名为“test”并且我的显示被命名为“block”,我可以用文件覆盖
views-view--test--block.tpl.php
第三件事,模板不能包含函数调用,它会产生一些性能问题,这不是一个好习惯。只能$variables
是echo
or printed
。你可以看一下views module
,在“themes”文件夹下就明白了。
最后,如果您需要在渲染之前修改一些数据,您有几个可用的挂钩custom modules
或template.php
允许您修改它。
希望它可以帮助你:)
推荐阅读
- c - 使用防御性编程初始化结构数组的问题
- java - 无法识别的字段:jerseyClient dropwizard yml?
- azure-ad-graph-api - AADSTS50034:xxx.onmicrosoft.com 目录中不存在用户帐户 Microsoft.AzureAD.Telemetry.Diagnostics.PII
- javascript - 使用javascript函数调用ajax来更新MYSQL结果
- php - TCPDF 中的自定义 HTML 页脚
- groovy - 从测试套件拆卸脚本访问测试用例属性
- matlab - 如何减少 xcorr 函数的执行时间?
- typescript - tsc 在 dist 中创建 src 文件夹
- r - 如何从 13:24:00 开始对变量进行分组并将时间缩短为 10 秒的时间段,并为 group_by 变量取平均值
- typescript - 如何解决错误“TS2532:对象可能是“未定义”?