1

我正在使用 Drupal 5,并且有很多我想更改其输出的视图。使用视图向导,我可以为每个实例创建一个不同的模板,但我想对我的所有视图进行相同的更改,并且主题目录中有 30 个文件接缝,就像大量的维护和代码一样。有谁知道是否有一种默认方式可以同时处理所有视图,然后使用我目前拥有的一次性使用?

这是我现在拥有的:

意见列表-首页________文章_______latest.tpl.php

<?php 
/**
 * views template to output one 'row' of a view.
 * This code was generated by the views theming wizard
 * Date: November 17, 2008 - 2:07pm
 * View: home_articles_latest
 *
 * Variables available:
 * $view -- the entire view object. Important parts of this object are
 *   home_articles_latest, .
 * $view_type -- The type of the view. Probably 'page' or 'block' but could
 *   also be 'embed' or other string passed in from a custom view creator.
 * $node -- the raw data. This is not a real node object, but will contain
 *   the nid as well as other support fields that might be necessary.
 * $count -- the current row in the view (not TOTAL but for this page) starting
 *   from 0.
 * $stripe -- 'odd' or 'even', alternating. * $title -- Display the title of the node.
 * $title_label -- The assigned label for $title
 * $comment_count -- This will display the comment count.
 * $comment_count_label -- The assigned label for $comment_count
 * $field_abstract_value -- 
 * $field_abstract_value_label -- The assigned label for $field_abstract_value
 *
 * This function goes in your views-list-home_articles_latest.tpl.php file
 */


 //now we add the stylesheet...
 //drupal_add_css(path_to_theme() .'/views-list-home_articles_latest.css');

  ?>
  <?php print $view ?>
<div class="view-label view-field-title">
  <?php print $title_label ?>
</div>
<div class="view-field view-data-title">
  <?php print $title?>
</div>

<?php if ($comment_count != '0' && $view_type == 'block'): ?>
<div class="view-label view-field-comment-count">
  <?php print $comment_count_label ?>
</div>
<div class="view-field view-data-comment-count">
  <?php print $add?><?php print $comment_count?>
</div>
<?php endif; ?>

<?php if ($count == 0): ?>
<div class="view-label view-field-field-abstract-value">
  <?php print $field_abstract_value_label ?>
</div>
<div class="view-field view-data-field-abstract-value">
  <?php print $field_abstract_value?>
</div>
<?php endif; ?>

在模板.php

/**
 * views template to output a view.
 * This code was generated by the views theming wizard
 * Date: November 17, 2008 - 2:07pm
 * View: home_articles_latest
 *
 * This function goes in your template.php file
 */
function phptemplate_views_view_list_home_articles_latest($view, $nodes, $type) {
  $fields = _views_get_fields();

  $taken = array();

  // Set up the fields in nicely named chunks.
  foreach ($view->field as $id => $field) {
    $field_name = $field['field'];
    if (isset($taken[$field_name])) {
      $field_name = $field['queryname'];
    }
    $taken[$field_name] = true;
    $field_names[$id] = $field_name;
  }

  // Set up some variables that won't change.
  $base_vars = array(
    'view' => $view,
    'view_type' => $type,
  );

  foreach ($nodes as $i => $node) {
    $vars = $base_vars;
    $vars['node'] = $node;
    $vars['count'] = $i;
    $vars['stripe'] = $i % 2 ? 'even' : 'odd';
    foreach ($view->field as $id => $field) {
      $name = $field_names[$id];
      $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
      if (isset($field['label'])) {
        $vars[$name . '_label'] = $field['label'];
      }
    }
    $items[] = _phptemplate_callback('views-list-home_articles_latest', $vars);
  }
  if ($items) {
    return theme('item_list', $items);
  }
}

谢谢,
史蒂夫

4

3 回答 3

2

认为只创建一个名为“views-list.tpl.php”的文件将适用于所有列表样式的视图(除非存在更具体的 .tpl.php 文件)。

否则,可能有一种方法可以使用主题功能获得您想要的东西。

于 2008-11-25T02:35:03.843 回答
0

在 Views 5 中,默认情况下模块无法公开 tpl 文件,因此模板名称对您没有任何帮助。关键是要放入你的 template.php 文件中的函数,“phptemplate_views_view_list_home_articles_latest”。这是一个 PHP 函数,它拦截任何名为“home_articles_latest”的列表视图的呈现。如果您希望它拦截所有视图,只需将函数本身的名称更改为:“phptemplate_views_view”

请记住,这会影响一切——使用视图、RSS 提要等构建的侧边栏块。

于 2009-03-01T00:06:52.227 回答
0

我相信每个模板都必须独立更新,如果您谈论的是视图模板内部发生的事情。我不知道只更新 n 个视图的一小部分的方法。

如果您在每个视图的模板上方都有一个标题,您可以使用 template.php 中的辅助函数或 node.tpl.php 中的条件来做到这一点。但如果它比那更远,我相信你不走运。

于 2008-11-19T17:20:52.543 回答