Suggested template files for views

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by degeer on January 15, 2009 at 3:51pm

I'm work on a site where we are using views to display various content. Our front-end developers have been instructed to work with tpl-files that are put in our theme. They have to be able to create template files in the same manor as they do for page, node, blocks - e.g. page-node-1.tpl.php, page-node.tpl.php etc.

I've come up with a solution where I re-use code from http://api.drupal.org/api/function/template_preprocess_node/6

Views

function phptemplate_preprocess_views_view( & $vars)
{
$i = 0;
$suggestion = 'views-view';
$suggestions = array ();
while ($arg = arg($i++))
{
$suggestions[] = $suggestion.'-'.$arg;
if (!is_numeric($arg))
{
$suggestion .= '-'.$arg;
}
}

if ($suggestions)
{
$vars['template_files'] = $suggestions;
}
}

Views-table

function phptemplate_preprocess_views_view_table( & $vars)
{
$i = 0;
$suggestion = 'views-view-table';
$suggestions = array ();
while ($arg = arg($i++))
{
$suggestions[] = $suggestion.'-'.$arg;
if (!is_numeric($arg))
{
$suggestion .= '-'.$arg;
}
}

if ($suggestions)
{
$vars['template_files'] = $suggestions;
}
}

Questions

  1. Is there a better way to do this without creating these two functions in template.php?
  2. If there isn't a better way - shouldn't these functions be put into views core?

Comments

view templates

Posted by pasqualle on January 15, 2009 at 6:32pm

1. Is there a better way to

Posted by merlinofchaos on January 15, 2009 at 6:38pm

1. Is there a better way to do this without creating these two functions in template.php?

Nope.

2. If there isn't a better way - shouldn't these functions be put into views core?

No. It is very very confusing to mix the pattern style of alternative templates and the suggestions style. It's ok for you to do that in your theme, if you know what you're doing, but I will not be publishing a module that does it. I don't want the support requests.

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /