Dynamic themes in D6, theming a view

luksedj - May 5, 2008 - 21:51

Hi,

After being away for a couple of years I'm back into Drupal again. Jumping into D6, I'm having some trouble in finding appropriate documentation regarding the dynamic theming functionality.

I installed the views module, created a view, which is available as a block. Now I want to style the block.

On the views admin page, I can see for each part of the view which theme functions will be used, e.g (my block is called NewsBlock):

* Display output: views-view.tpl.php, views-view--NewsBlock.tpl.php, views-view--default.tpl.php, views-view--NewsBlock--default.tpl.php
* Style output: views-view-unformatted.tpl.php, views-view-unformatted--NewsBlock.tpl.php, views-view-unformatted--default.tpl.php, views-view-unformatted--NewsBlock--default.tpl.php
* Row style output: views-view-fields.tpl.php, views-view-fields--NewsBlock.tpl.php, views-view-fields--default.tpl.php, views-view-fields--NewsBlock--default.tpl.php
* Field Node: Title: views-view-field.tpl.php, views-view-field--NewsBlock--title.tpl.php, views-view-field--default.tpl.php, views-view-field--default--title.tpl.php, views-view-field--NewsBlock--default.tpl.php, views-view-field--NewsBlock--default--title.tpl.php
* Field Node: Body: views-view-field.tpl.php, views-view-field--NewsBlock--body.tpl.php, views-view-field--default.tpl.php, views-view-field--default--body.tpl.php, views-view-field--NewsBlock--default.tpl.php, views-view-field--NewsBlock--default--body.tpl.php

(I guess there is a bug in here as well, the list of template files keeps growing for each list item, but that is not really relevant to my question)

I see, reading about dynamic theming for modules, that I probably have to create e.g. a views-view-fields--NewsBlock.tpl.php.
I tried this (starting from a copy of views-view-fields.tpl.php) but to no avail. Either I'm forgetting something, or I'm putting the file in the wrong place.
(I've put it under modules/views/theme/)

Anybody any suggestions? If my question is not clear, please ask...

Thanks in advance,

luksedj

Also having trouble with view theming in 6.2

stevek@virtusta... - May 19, 2008 - 12:33

Changing view-view-fields.tpl.php works, but I cannot figure out what to do to override this default for a specific block.

I'm having trouble too!

stinky - May 18, 2008 - 23:39

Once upon a time I figured out how to theme views (table format) in Drupal 5.7, but the same things isn't working in 6.2. I have no idea where to start and have spend the entire day looking at documentation, but I don't see any good docs for 6.2. I'm willing to write docs if someone could give me a hint on how to theme views in table format. Do I even still use phptemplate and put the call in my template.php file?

subscribing

kylehase - June 9, 2008 - 07:45

subscribing

subscribing

afandyag - June 25, 2008 - 13:04

subscribing

subscribing

FdF - June 20, 2008 - 14:11

subscribing

subscribing

waynesmifff - June 21, 2008 - 10:02

subscribing

subscribing

o0avb0o - June 25, 2008 - 13:00

subscribing

subscribing

rich.yumul - June 26, 2008 - 11:44

subscribing

subscribing

FLSH - July 10, 2008 - 13:09

subscribing

subscribing. I crate a copy

lancelot - July 14, 2008 - 11:55

subscribing.

I crate a copy of views-view-fields.tpl.php, name it views-view-fields--myview.tpl.php. Then place it to themes/mytheme/ folder as it doesn't work in views/theme/.
On the page of 'myview' I see that this template is applied (I add some debug output there), but the variables ($view,$fields,$rows) are empty

The same situation with

lancelot - July 14, 2008 - 11:57

The same situation with mytheme_views_view_fields__myview function which I try to put in mytheme/template.php

same problem

ajross - August 18, 2008 - 18:13

Subscribing. I'm having the same problem as lancelot - all my variables are empty after putting view-view--myviewname.tpl.php in my theme directory. Anybody else having this problem or know of a solution?

Subscribing

mestato - September 9, 2008 - 21:22

Same problem here. My templates are being recognized in my theme folder, but returning "invalid argument" when I try to use $fields. I tried printing the array of variables (print htmlspecialchars(print_r(get_defined_vars(), TRUE), ENT_QUOTES);) - you can get to the view data through the array but its a pain. For example:
$view->result[0]->nid

Solved

luksedj - July 14, 2008 - 16:02

Ok,

I missed to put the overrides in the folder of my theme. I thought the files should go in modules/views/theme/, but that was a mistake...

i am getting success by: 1.

xmvcnr - August 12, 2008 - 14:50

i am getting success by:

1. downloading /modules/views/theme/views-view-field.tpl.php

2. editing it locally. and RENAMING it from views-view-field.tpl.php to
views-view-field--ViewName--fieldname.tpl.php
(e.g. views-view-field--ExpertsListView--title.tpl.php where I am trying to modify the title field in a view called ExpertsListView)

3. uploading the renamed file, NOT to where it came from, but to the /themes/garland/ directory.

4. using web admin to do Administer > Site building > Views > (Edit) > (Theme Information) > Rescan, OK, Save

i am using Drupal 6.3, 2008-07-09
and Views 2.0-dev (8/12/07?)
both downloaded recently.

---

One question is, why does the file need to change directories?

---

BUG?:

While I am getting success with e.g. views-view-field--ExpertsListView--title.tpl.php
i am NOT getting success with views-view-field--default--title.tpl.php

The changes are reflected in the Preview, but not on the live site (even after I click the Save button!)

Some tips on how this works

Herman Hiddema - September 12, 2008 - 15:04

I had a lot of trouble with this too, but I figured most of it out in the end (mostly by reading code). An example:

I have a CCK content type called registration, allowing people to register for an event. The content type allows them to enter information like their name, email, country, etc.
I have a view to show a list of registered people, which is called registered.

In the list of registered people, I wanted to show flags for countries instead of just the name. To do this, I created the following file:
sites/all/themes/mytheme/views-view-field--registered--field-country-value.tpl.php

With the content:

<?php
$data
= $row->{$field->field_alias};
if (
$output && $data) {
        print
'<img src="/sites/default/files/icons/gif/'.$data.'.gif" alt="'.$output.'" title="'.$output.'"/>';
}
else {
        print
'<img src="/sites/default/files/icons/bullet_error.png"/>';
}
?>

The input field field_country is a select list of countries, with key/label pairs, eg: "us|United States of America", "de|Germany", "ja|Japan", etc.
In the above code the retrieved $data contains the key (which is what is stored in the database), and the variable $output contains the full name of the country (as already formatted by Drupal, $output would be the output if this theming file wasn't here.

So the end result is that instead of showing the name of the country, a flag is show (eg us.gif, de.gif, ja.gif) which shows the name of the country as a tooltip (hence the title attribute) or prints the country name is the image is not found (or someone has images turned of).

This is an example of how to theme a specific field. There are similar filenames for theming the look of all fields (views-view-field--VIEWNAME.tpl.php) your tables (views-view-table--VIEWNAME.tpl.php), etc. The advanced_help module is very helpful here, showing the possible file names

The basic way to go about this is to take the corresponding file from /sites/all/modules/views/theme, copy it to your own theme directory, change the file name to be as specific as you need it, and adjust it to your needs.

Keep in mind that you need to clear drupal's cache before things will work sometimes!
To do this, go to "Administer › Site configuration › Performance" and click the "Clear cached data" button at the bottom.

Admin theme

magnus.ramon - September 15, 2008 - 18:28

[...] copy it to your own theme directory [...]

Just adding a warning: if you use a different theme in the administration pages (while developing your own theme or not to waste time on that part of it), Views will look for the tpl files in the administration theme's directory.

So, or you use the administration theme to host your custom Views tpl files or you change your strategy.

| Ramon Magnus
|>>> NRDS <<<
|:: magnus.ramon@nrds.com.br
|:: magnus.ramon@gmail.com

In this post you can find

mortenson - November 10, 2008 - 14:29

In this post you can find how to do dynamic text colouring:

http://drupal.org/node/302856

 
 

Drupal is a registered trademark of Dries Buytaert.