Any reasonably complex Drupal website involves using Views for listing items and CCK for the creation of custom content types. Since each website has a unique look, a large part of creating a Drupal website is theming Views and CCK nodes. This is especially true for Views, since most CCK theming can be done via CSS (or for more complex stuff it goes to Forms API, beyond theming), but Views usually needs to be themed.
Drupal theming has changed with Drupal 6 and so has the Views theming with Views 2 that ships with Drupal 6. Below is a quick introduction to the new theming approach in Drupal 6 and Views 2.
We assume that readers are intimately familiar with Views theming in Drupal 5, hence we will only highlight the differences.
Most views theme functions, like the familiar theme_views_view_VIEWNAME($view, $type, $nodes, $level = NULL, $args = NULL) are now gone! New theming functions are as follows:
Recordset theming:
theme_views_view_unformatted__VIEWNAME($view, $options, $rows, $title);
theme_views_view_list__VIEWNAME($view, $options, $rows, $title);
theme_views_view_grid__VIEWNAME($view, $options, $rows, $title);
theme_views_view_table__VIEWNAME($view, $options, $rows, $title);
theme_views_view_summary__VIEWNAME($view, $options, $rows, $title);
theme_views_view_summary_unformatted__VIEWNAME($view, $options, $rows, $title);
theme_views_view_rss__VIEWNAME($view, $options, $rows, $title);Individual item theming:
theme_views_view_row_comment__VIEWNAME($view, $options, $row);
theme_views_view_row_node__VIEWNAME($view, $options, $row);
theme_views_view_row_search__VIEWNAME($view, $options, $row);
theme_views_view_row_fields__VIEWNAME($view, $options, $row);And special theming functions for:
theme_views_exposed_form__VIEWNAME ( $form );
theme_views_more__VIEWNAME($more_url);As you can guess, the starting "theme" in the function name should be substituted by theme name, if you are implementing it in a theme, or with "phptemplate" if you are implementing in a module
CAUTION: Views2 is heavily cached, so you need to clear cache after any change (like adding a new theme function or modifying theme type + function) to see the updated results. Installing Devel module helps.
***UPDATE: tpl.php theming***
By popular demand (heh), we are adding couple words about theming Views2 from tpl.php files:
To get examples of tpl.php implementations you don’t need a documentation it’s right there in front of you (ok, maybe a little bit hidden)
When you are in a view edit page, click on “Theme: Information” under Basic settings, it will show you all possible template files that the view could use (depending on – what level do you wish to override on). The tpl.phps that are actually being used will be in bold.
For each set the title of set is hyprlinked (e.g. “Row style output”). Click on that and you will see a sample tpl.php of that group/set. Those are extremely helpful, pretty self-explanatory, too.
You can also find these tpl.php’s in the views module, under the “theme” folder. I think you will like what you find there.
* * *


Delicious
Digg
StumbleUpon
Reddit
Technorati
Any idea why
phptemplate_views_view_unformatted_whilephptemplate_views_view_row_fields_doesn't?I used to be able to access each field within the views template file through the wizard, so I could wrap those variables in html and set up the template that way.
Now I can’t understand how to gain access to each individual field for each row…any ideas?
Views2 Templates are well-documented at this URL:
http://views.doc.logrus.com/group__views__templates.html
and the variables you can use in these template files are described at:
http://views.doc.logrus.com/views-view-fields_8tpl_8php.html
and
http://views.doc.logrus.com/views-view_8tpl_8php.html
You can also find default implementations of the template files under the “theme” subfolder of the views module package. They are good examples to start with.
Are there any tricks to getting these functions to work inside template.php I have tried the naming conventions above with out any joy. I am trying to convert some functions from D5 to D6 get the function to be called. Any help would be appreciated.
TIA, Mic
These overrides work in template.php, but you have to make sure to bounce your cache.
If the name of your theme is “golden”, the name of your view is “pet_listing” and the format (the one you indicate when you create view) is “grid”, you can create a function in template.php that will be named:
phptemplate_views_view_grid__pet_listing($view, $options, $rows, $title)or
golden_views_view_grid__pet_listing($view, $options, $rows, $title)
If you are doing hardcore theming of views in your template, you probably want to choose format called “unformatted”, because standard formatters will only get in your way. In such case, the functions you can create are:
phptemplate_views_view_unformatted__pet_listing($view, $options, $rows, $title)or
golden_views_view_unformatted__pet_listing($view, $options, $rows, $title)
Both names will work, but by convention since first (starting with phptemplate_) is more generic, you should use such names in modules, whereas the latter has the name of your theme and you should use that in themes.
The last important piece is to clear cache. You can easier install and use the Devel module for that, or run the following commands directly in the database:
truncate views_object_cache;truncate cache_views;
truncate cache;
How about some hard core examples? The lack of examples and hard core documentation (with examples) is holding Views2 back big time – frankly Views1 was a walk in the part to theme, but Views2 is mad, mad, mad – it not easy to get theme the contents of the output variable, and your guide offers nothing to change that – the template system is SLOW beyond belief (these guys actually expect that using 4 or 5 tpl files to theme a single view is a “great leap forward”)... one would have thought you could use a preprocess function or a theme function (looks like you can, but ZERO documentation exists). At this stage I don’t care how “great” Views2 is supposed to be – for most of us, its next to useless without a basic theme guide (simple so you don’t need a degree in Views2 to follow…).
To get examples of tpl.php implementations you don’t need a documentation it’s right there in front of you (ok, maybe a little bit hidden)
When you are in a view edit page, click on “Theme: Information” under Basic settings, it will show you all possible template files that the view could use (depending on – what level do you wish to override on). The tpl.phps that are actually being used will be in bold.
For each set the title of set is hyprlinked (e.g. “Row style output”). Click on that and you will see a sample tpl.php of that group/set. Those are extremely helpful, pretty self-explanatory, too.
You can also find these tpl.php’s in the views module’s under the “theme” folder. I think you will like what you find there.
I can very well understand your frustration since Views2 is very different from Views1, and somewhat more complex too, until you get used to it. Still, with all fairness, Views2 is a HUGE leap forward and once you get well-familiarized with it – it is absolutely awesome.
It’s not slower since theming is highly cached and queries Views2 produces are order of magnitude more optimized than whatever Views1 used to spit out.
Some patience, some perseverance and you will get to the point when you love it :)
No mention at all of templates? That’s unfortunate.
The caching here isn’t Views, it’s the theme registry; so what you say is true whenever you add a new theme function or template, regardless of whether or not it’s Views.
In general this article feels very incomplete. It teases nicely but it doesn’t really deliver a lot.
But templates are already well documented (though perhaps they weren’t at time of writing?) whereas this is not. I link to the template documentation, now that it’s live, would be a worthwhile edit though. This page stands to get very popular, since I can’t find any info anywhere else on this particular approach to theming. =)
Hey Earl,
nice to see some heavy-weight Views power on this turf :)
Your points are well-taken, but this blog post is exactly what you said it is – a teaser. Just to give a taste to curious readers :) If it was complete, it would be in Drupal handbook, not on a blog, eh?
cheers
All your comments above ppl are correct. I indeed combing the net to find decent and full information about template.php views2 theming functions to find almost nothing to this date.
I wish there was some good documentation on this issue in the official documentation like there is (as noted) for template files usage. I’ll be happy to receive such links if you spot such!
In the meantime, thanks Irakli for this “teaser-post” :-)
Boaz.