drupal视图中创建的行太多


too many rows created in drupal view

我已经修改了用于网格视图的tpl以显示3列内容,我唯一的问题是下面的代码为视图创建了不必要的div。我有最多9个项目,应该在3行中输出,每列3个。修改下面代码的最佳方法是什么?以防止输出额外的div。

<?php foreach ($rows as $row_number => $columns): ?>
  <div>
    <?php foreach ($columns as $column_number => $item): ?>
        <?php print $item; ?>
    <?php endforeach; ?>
  </div>
<?php endforeach; ?>

我会删除foreach语法的符号(但我认为这是非常个人的:-))

你可以使用模来检查你是否只有3列(如果我理解你的问题)。

<?php
// I´m assuming that $column_number is a zero based index
// if thats not the case you should add a counter to keep track of column numbers or if it is in sequence but isn't zero based you could easily update the calculates based in your starting index
foreach ($rows as $row_number => $columns) {
    foreach ($columns as $column_number => $item) {
        if ($column_number == 0 || $column_number%3 == 0) {
            print '<div>';
        }
        print $item;
        if ($column_number == 2 || $column_number%3 == 2) {
            print '</div>';
        }
    }
    // prevent open div tags
    $total_columns = count($columns);
    if ($total_columns > 0 && ($total_columns < 3 || $total_columns%3 != 0)) {
        print('<div>');
    }
}

我还去掉了所有php的开始和结束标签,以提高可读性。