Can we please get a simple how-to of getting a view themed without the theming wizard (e.g. wrapping content around the data via a template)? There are many examples for 1.x, but they all seem to break in 2.

Comments

merlinofchaos’s picture

Status: Active » Fixed

Sure. Look in the 'themes' directory in Views. Now, right now you have to guess what template you're using, but I'll create a tool that will tell you. That's on my mental TODO list. However, it's not TOO ahrd, as the theme templates largely match the style name.

There will be a template for the style and a template for the row style (if the style uses one). Copy that template into your theme. Modify to suit. They are reasonably well documented, I think.

paul-at-murphymaphia.com’s picture

Status: Fixed » Active

This doesn't really seem to work at all.

Copied views-view-list.tpl.php into my theme directory (basedirectory/themes/nameoftheme/ right?), modified, cleared the cache... no results.

It also isn't clear how to override, for example, the list output for a specific view rather than the general output it seems these tpl files are meant to theme.

FWIW, I appreciate the hard work the dev team is doing on this module. I'd love to even help document some of this functionality if I could manage to figure it out. :)

merlinofchaos’s picture

Sorry, yes. There's a plethora of names that a given template could possibly have. The easiest one would be, in the case of the list template:

views-view-list--VIEWNAME.tpl.php

I am surprised, though; copying that to your theme directory, and then clearing the cache...it should have gotten all views set to the 'list' style. I admit I haven't actually done much testing of theming, but the theming system in D6 is supposed to work.

Here's a little piece of code that might help:

function _views_theme_functions($hook, $view, $display = NULL) {
  $themes = array();

  if ($display) {
    $themes[] = $hook . '__' . $display->id . '__' . $view->name;
    $themes[] = $hook . '__' . $display->id;
    if ($display->id != $display->display_plugin) {
      $themes[] = $hook . '__' . $display->display_plugin . '__' . $view->name;
      $themes[] = $hook . '__' . $display->display_plugin;
    }
  }
  $themes[] = $hook . '__' . $view->name;
  $themes[] = $hook;

  return $themes;
}

Pretty much every theme function is run through this algorithm.

So you should be able to theme views by display type, display id as well as view name. As I said, there really needs to be a tool to help identify which theme function will actually get used (though the theme developer tool in the devel.module might actually help with that).

paul-at-murphymaphia.com’s picture

that's helpful, thanks.

i was able to override the output as you suggested once I figured out the right tpl file to edit. Summary view trumps list view :)

however, once I tried to override a specific view (named authors) by naming the file views-view-summary--authors.tpl.php each row returned no data for all the vars in the tpl file. That is to say, all the html from the tpl file was output but the vars were empty. Do I need to do some kind of callback in the template.php like in D5?

merlinofchaos’s picture

You shouldn't need to! Hm. I wonder if maybe the preprocess isn't getting called. That would be really weird, though.

And yes, the summary view absolutely trumps the list view. It's actually a totally different style. I'll have to mess with this and see. It may be a bug in Drupal core.

paul-at-murphymaphia.com’s picture

What it looks like is that the $rows object is there but seems to have been pre-processed differently than when being handled by default.

using print_r($rows) I am able to see the structure of the object and do my loop.

I admit that this may be expected behavior and I'm just not clued into that. Slowly but surely I'm figuring this out. Thanks for everything you're doing on this module.

merlinofchaos’s picture

Ok, there is a bug in Drupal core. I worked up a patch here: http://drupal.org/node/241570

That should solve the problem.

paul-at-murphymaphia.com’s picture

Brilliant. I think I've got the hang of it.

mholger’s picture

For the life of me I cannot get this to work. I'm running Drupal 6.2, views 6.x-2.0-dev, and in my theme directory I have:
views-view.tpl.php
views-view-unformatted.tpl.php
views-view-unformatted--frontpage.tpl.php

Rendering yields nada. In fact, from some tracing in drupal core, it appears that these files aren't even being referenced, let alone loaded and processed...

I dug around a bit, and found that if I add an entry (say, 'views-view-unformatted.tpl.php') to the $vars['template_files'] array in template_preprocess_views_view() inside modules/views/theme/theme.inc, *then* the file gets processed.

Sadly, I'm too much of a Drupal/views newb to be able to figure out what's supposed to be happening where in order to come up with a good solution... :(

merlinofchaos’s picture

If you haven't applied the patch in http://drupal.org/node/241570 I will scold you.

mholger’s picture

That was the very first thing I did after discovering that it appeared my views-view-* files weren't being processed.. :)

$ patch -p0 <fix-preprocess-patterns.patch
patching file includes/theme.inc
Hunk #2 succeeded at 733 (offset 3 lines).
Hunk #3 succeeded at 827 (offset 3 lines).
$

-- No joy. :(

merlinofchaos’s picture

Hmm. I have it verified working on my test system, too. I assume you've cleared cache?

mholger’s picture

Well dang. The scolding may commence...

I had explicitly disabled caching (or so I thought), so I hadn't bothered attempting to find a cache to clear.

Deleting the contents of the cache table in my database seems to have rectified the issue, however.

I thank you much both for your time, modules, and patience! :)

merlinofchaos’s picture

In Drupal 6, new theme templates like this won't be seen until the theme registry cache is cleared. It can always be cleared by visiting admin/build/theme

merlinofchaos’s picture

FYI I'm currently implementing a theme analysis tool that will help alleviate problems like that.

merlinofchaos’s picture

Status: Active » Fixed

Said tool is implemented and (gasp) documented. Hopefully that answers all questions on the topic.

ansorg’s picture

It can always be cleared by visiting admin/build/theme

seems to be not true.

I did not get my new theme functions to work until I cleared the drupal cache (although cache is not even enabled)

But after doing that I get my theme to do what I want - finally :)

thank you

datune’s picture

Version: 6.x-2.0-alpha4 » 6.x-2.0-beta2
Status: Fixed » Postponed (maintainer needs more info)

I can't get it to work.

If I copy the contents of views-view-fields.tpl.php, save the file in my themes directory as views-view-fields--myview.tpl.php, without making any changes to the content, I get the following
error after clearing the cache (which does not work when visiting admin/build/theme btw., it only works when visiting admin/build/modules)

warning: Invalid argument supplied for foreach() in /home/xxxxxxx/public_html/themes/garland/views-view-fields--myview.tpl.php on line 19.

Line 19: foreach ($fields as $id => $field):

When I var_dump the contents of $fields it returns NULL, am I missing something obvious here?

(Yes, I applied the patch you posted merlin)

Thanks in advance!

stinky’s picture

subscribe

stopsatgreen’s picture

Has this patch made it into Beta 3? Because I have the same problem; when I copy views-view-table--BLOCKNAME.tpl.php into my theme directory, the output returns no data.

I can't patch as I'm working on a Windows box which I don't have command line access to.

merlinofchaos’s picture

The patch is against Drupal core, not Views, and it will be in Drupal 6.3 but that is not available. You can download the -dev version of Drupal 6, though.

stopsatgreen’s picture

Ah. Damn. Will have to try to get someone to perform the patch for me.

stopsatgreen’s picture

Got access to the server, downloaded and installed Cygwin and tried implementing the patch; got this message:

Hunk #1 Succeeded at 347 with fuzz 2 .
Hunk #2 Failed at 732.
Hunk #3 Failed at 825.

The problem has not been resolved.

merlinofchaos’s picture

Status: Postponed (maintainer needs more info) » Fixed

So just download the 6.x-dev version, since the patch was committed.

jptaranto’s picture

Hmmm. it seems what i need isn't quite covered here. The old wizard used to give you tokens or variables you could then use to place

<?php 
/**
 * views template to output one 'row' of a view.
 * This code was generated by the views theming wizard
 * Date: Sat, 14/07/2007 - 08:24
 * View: random_portfolio_piece
 *
 * Variables available:
 * $view -- the entire view object. Important parts of this object are
 *   random_portfolio_piece, .
 * $view_type -- The type of the view. Probably 'page' or 'block' but could
 *   also be 'embed' or other string passed in from a custom view creator.
 * $node -- the raw data. This is not a real node object, but will contain
 *   the nid as well as other support fields that might be necessary.
 * $count -- the current row in the view (not TOTAL but for this page) starting
 *   from 0.
 * $stripe -- 'odd' or 'even', alternating.
 * $title -- Display the title of the node.
 * $title_label -- The assigned label for $title
 * $field_teaser_fid -- 
 * $field_teaser_fid_label -- The assigned label for $field_teaser_fid
 * $link -- This will create a link to the node; fill the option field with the text for the link. If you want titles that link to the node, use Node: Title instead.
 * $link_label -- The assigned label for $link
 *
 * This function goes in your views-list-random_portfolio_piece.tpl.php file
 */
  ?>
<div class="view">
<div class="teaserimg">
  <?php print $field_teaser_fid?>
</div>
<p><?php print $title?> | <?php print $link?></p>
<?php print $field_teaser_description_value ?>
</div>

I know how to use the theme files as per the "theme information" section in the new views interface - so i have created a file called "views-view-unformatted--featured--default.tpl.php" to format my "featured" view - which has 3 fields (much like the above example), a title, url and teaser image.

How can I call these fields directly in the tpl so I can theme the HTML output?

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

trofimchouk’s picture

Status: Closed (fixed) » Active

I use views-rc1, Drupal 6.3
views-view-fields--myview.tpl.php still doesn't work
$view, $row contains some information but $fields is still empty:
print_r($fields) : 'Array ( [inline] => Array ( ) [separator] => )'

I cleared all the cache: theme registry, views cache so on - just cleared all the cache tables in database. But no result.

merlinofchaos’s picture

Status: Active » Closed (fixed)

I'm sorry, I cannot duplicate this behavior. I get correct results when I do this. Are you absolutely, positvely, 100% sure you've actually got Drupal 6.3 installed with all of its code?

In any case, if it's not working, it's a core bug, not a Views bug.

ldutson’s picture

Status: Closed (fixed) » Active

I can't find the answer to this question anywhere either... how do you access fields in a view template? Is there some type of naming convention that allows you to access fields from a view?

merlinofchaos’s picture

Status: Active » Closed (fixed)

Then you're not looking very hard, as this very issue is concisely documented in the advanced help.

ldutson’s picture

Status: Closed (fixed) » Active

The answer to thedecline's question? I don't see any reference to this at all in advanced help:

Hmmm. it seems what i need isn't quite covered here. The old wizard used to give you tokens or variables you could then use to place

I know how to use the theme files as per the "theme information" section in the new views interface - so i have created a file called "views-view-unformatted--featured--default.tpl.php" to format my "featured" view - which has 3 fields (much like the above example), a title, url and teaser image.

How can I call these fields directly in the tpl so I can theme the HTML output?

merlinofchaos’s picture

Status: Active » Fixed

I am not lying. The topic is called "Using Views templates" and you are seriously trying my patience.

ldutson’s picture

Status: Fixed » Active

The problem is that the variable dumps return pre-styled strings, not array variables for the fields. The help documentation shows simple arrays being returned, but that is not what variable dumps in a views template seem to do.

string(465) "  
  
Bangor Losing Two Starbucks

  
  
field_link: http://www.asmainegoes.com/forum/viewtopic.php?p=608234#608234

  
  

"
merlinofchaos’s picture

Status: Active » Closed (fixed)

I'm sorry, you're wrong. If you're using the unformatted style, $row will contain rendered strings. I really don't understand why you're making this so hard. It's documented, and the data format is really simple. The data is there. It's documented, and yet you're just poking at this issue over and over again.

For the benefit of those of you unable to read, here is the damn documentation. Again.

// $Id: views-view-fields.tpl.php,v 1.5 2008/05/05 23:51:47 merlinofchaos Exp $
/**
 * @file views-view-fields.tpl.php
 * Default simple view template to all the fields as a row.
 *
 * - $view: The view in use.
 * - $fields: an array of $field objects. Each one contains:
 *   - $field->content: The output of the field.
 *   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
 *   - $field->class: The safe class id to use.
 *   - $field->handler: The Views field handler object controlling this field. Do not use
 *     var_export to dump this object, as it can't handle the recursion.
 *   - $field->inline: Whether or not the field should be inline.
 *   - $field->inline_html: either div or span based on the above flag.
 *   - $field->separator: an optional separator that may appear before a field.
 * - $row: The raw result object from the query, with all data it fetched.
 *
 * @ingroup views_templates
 */

This is your basic unformatted template. Each template has its own version of this documentation right there in the template. Do you see the documetnation for the $field structure that's there? Each item. Carefully explained. Output. Raw output. Class. The only thing that's not immediately obvious is how to get the IDs.

And yet, I documented that too, in the help file I mentioned above.

So the short answer: In this particular template, look in the $fields array.

But more importantly, do your own work. Stop expecting other people to do your work for you.

This issue is now closed. Any attempt to re-open it will result in me closing it without comment.