Hi,

I have created themed views, but I have multiple themed views, and my template.php is getting very long, as each time I am adding the code snippet from ThemeWizard to it.
I noticed that the only thing that is really different is the function call, so my question is how can I shorten it.

For example (a short template.php) of mine looks like the following:

/**
* views template to output a view.
* This code was generated by the views theming wizard
* Date: Mon, 01/14/2008 - 17:53
* View: halls
*
* This function goes in your template.php file
*/

function phptemplate_views_view_list_halls($view, $nodes, $type) {
$fields = _views_get_fields();

$taken = array();

// Group our nodes
$set = array();
foreach ($nodes as $node) {
$set[$node->node_title][] = $node;
}

// 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,
);

$output = '';
foreach ($set as $label => $nodes) {
$items = array();
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-halls', $vars);
}
if ($items) {
$output .= theme('item_list', $items, $label);
}
}
return $output;
}

/**
* views template to output a view.
* This code was generated by the views theming wizard
* Date: Tue, 01/15/2008 - 10:36
* View: BarServices
*
* This function goes in your template.php file
*/
function phptemplate_views_view_list_BarServices($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-BarServices', $vars);
}
if ($items) {
return theme('item_list', $items);
}
}

Can some please explain how I can shorten this, or call many functions and use the code below for all of them.

Thanks in advance.

Comments

andjsmit’s picture

Once you have your first function in place, you can call this function for your similar views inside template.php :

function phptemplate_views_view_list_BarServices($view, $nodes, $type) {
  return phptemplate_views_view_list_halls($view, $nodes, $type); 
}