如何更改 drupal 5 视图模块的 HTML
我正在使用 Drupal 5,并且有大量想要更改其输出的视图。 使用视图向导,我可以为每个实例创建不同的模板,但我希望对所有视图进行相同的更改,并且主题目录中有 30 个文件,这需要大量的维护和代码。 有谁知道是否有一种默认方式可以同时处理所有视图,然后一次性使用我当前拥有的内容?
这就是我现在拥有的:
views-list-home_______articles_______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; ?>
template.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);
}
}
谢谢,
史蒂夫
I'm using Drupal 5 and have a multitude of views that I want to alter the output of. Using the views wizard, I can create a different template for each instance, but I'm wanting to do the same changes across all my views and having 30 files in the themes directory seams like a hell of a lot of maintenance and code. Does anyone know if there's a default way of addressing all the views at the same time and then using what I currently have for one offs?
Here's what I have now:
views-list-home_______articles_______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; ?>
in template.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);
}
}
Thanks,
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为只需创建一个名为“views-list.tpl.php”的文件即可应用于所有列表样式视图(除非存在更具体的 .tpl.php 文件)。
否则,可能有一种方法可以使用主题功能来获得您想要的东西。
I think just creating a file named "views-list.tpl.php" will apply to all List-style views (unless a more specific .tpl.php file is present).
Otherwise, there may be a way to get what you want using theme functions.
我相信,如果您正在谈论视图模板内发生的事情,则每个模板都必须独立更新。 我不知道有什么方法可以只更新 n 个视图中的一小部分。
如果您将一个标头放置在每个视图的模板上方,则可以使用 template.php 中的辅助函数或 node.tpl.php 中的条件来完成此操作。 但如果比这更深入的话,我相信你就不走运了。
I believe that each template will have to be updated independently, if you're talking about something that goes on inside the view's template. I don't know of a way to update just one small part of n views.
If you have a header that you're putting above the template for each view you might be able to do that with a helper function in the template.php or a conditional in node.tpl.php. But if it's further inside than that, I believe you're out of luck.
在 Views 5 中,默认情况下模块无法公开 tpl 文件,因此模板名称对您没有任何帮助。 关键是放入 template.php 文件中的函数“phptemplate_views_view_list_home_articles_latest”。 它是一个 PHP 函数,拦截任何名为“home_articles_latest”的列表视图的呈现。 如果您希望它拦截所有视图,只需将函数本身的名称更改为:“phptemplate_views_view”
请记住,这会影响所有内容 - 使用视图、RSS 提要等构建的侧边栏块。
In Views 5, there's no way for modules to expose tpl files by default, so the template name won't help you out any. The key is the function that's being put into your template.php file, "phptemplate_views_view_list_home_articles_latest". It's a PHP function intercepting the rendering of any list view named 'home_articles_latest'. If you wanted it to intercept ALL views, you'd just change the name of the function itself to: "phptemplate_views_view"
Keep in mind that would affect everything -- sidebar blocks build with views, RSS feeds, etc.