Tips for views theming, getting the row id within the *-fields template, getting the total number of rows a view has found

Here's a couple tips for views theming, great fun! Also an opportunity for the longest post title in the world.

First, How to get the row id from within the fields template.

When doing your own custom views theming, it can be useful to know which row number you're currently working with. A good example of this usage would be super-sizing certain elements for the first row of a listing.

To begin let's take a look at the default fields template views-view-fields.tpl.php.

<?php
/**
* @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->wrapper_prefix: A complete wrapper containing the inline_html to use.
*   - $field->wrapper_suffix: The closing tag for the wrapper.
*   - $field->separator: an optional separator that may appear before a field.
*   - $field->label: The wrap label text to use.
*   - $field->label_html: The full HTML of the label to use including
*     configured element type.
* - $row: The raw result object from the query, with all data it fetched.
*
* @ingroup views_templates
*/
?>

<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <?php print $field->wrapper_prefix; ?>
    <?php print $field->label_html; ?>
    <?php print $field->content; ?>
  <?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>

At the top of the file you've got a listing of the variables available to you, very useful! But there is an extra variable not listed: $id.

At a guess, it's not mentioned as the variable name is re-used in the foreach loop, before this point you can re-assign it or re-use it.

Depending on the nature of theming we are carrying out, sometimes it's necessary to have a specific HTML structure to each row, here's an example of this, making use of $id to add an extra class to the first item.

<?php
// $Id$
/**
* @file views-view-fields.tpl.php
*/

$wrapper_classes = 'wrapper';

if (
$id == 0) {
   
$wrapper_classes .= ' supersize';
}
?>

<div class="<?php print $wrapper_classes; ?>">
  <div class="left image">
  <?php print $fields['field_image_fid']->content; ?>
  </div>
  <div class="left text">
  <h2><?php print $fields['title']->content; ?></h2>
  <?php print $fields['teaser']->content; ?>
  </div>
</div>

Second, How to tell how many total rows a view has found, not just the number it's currently displaying.

This one's actually really easy, where-ever you have a view, in a view template, when running a view in code, you can do:

<?php
$view
= views_get_view('view_machine_name');
$view->execute();

$view->total_rows;
?>

One caveat, for this to work, your view needs to be using a pager. If you're not there are other ways, but it's a bit more involved.