More on views theming simple lists
<< Go back | Posted: Friday, August 22 2008 - 20:46
If you are like me, after a while you find yourself creating lots of views. Quite a few of these are simple lists themed in exactly the same way. Instead of using the theme wizard and adding the whole function to the template.php file every time, wouldn't it be better to just call another function with an argument to specify a template file to use?
function phptemplate_views_view_list_YOURVIEWNAME($view, $nodes, $type) {
return theme('standard_list', $view, $nodes, $type, 'views-list-testview');
}
function theme_standard_list($view, $nodes, $type, $template_file) {
$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($template_file, $vars);
}
if ($items) {
return implode('', $items);
}
}I set it up here so the view list functions call another function called theme_standard_list, and pass a template file argument to it. It saves on space and code.