User login

How to theme (lots of) views the Agaric Way

I3IVIIVI: now, take a look at the template.php file
(10:20:54 AM) Dan Hak: you took out repeating code?
(10:21:15 AM) I3IVIIVI: yes, exactly
(10:21:22 AM) I3IVIIVI: the code that you knew was repeating
(10:21:35 AM) I3IVIIVI: because you weren't even copying it from the wizard anymore
(10:21:49 AM) I3IVIIVI: you were just changing that one spot :-)
(10:22:11 AM) I3IVIIVI: it was actually harder to refactor than I thought. I shouldn't have done it now
(10:22:50 AM) I3IVIIVI: So you can see the series of functions that you just have to copy and change in two places to make a call to another template
(10:23:39 AM) I3IVIIVI: we have to set up equal test environments and do a performance test with another setup, where views will simply use a template *if* it exists
(10:24:17 AM) I3IVIIVI: if that has decent performance that's really nice, don't have to touch template.php, just make a new views-list-name.tpl.php

Resolution

In template.php – the generic function followed by an example of a specific invocation. Automatically using a views-list-thememe.tpl.php file if it exists is not implemented with this code.

<?php
/**
 * Generic function to theme views, invoked by view-specific callbacks.
 *
 * Based on code generated by the views theming wizard
 * Credit to I think Jacob Redding for a NYC DrupalCamp presentation on this
 * This function goes in your template.php file
 */
function phptemplate_views_template($template, &$view, &$nodes, &$type) {
  $fields = _views_get_fields();
  $taken = array();

  // Set up the fields in nicely named chunks.
  foreach ($view->field as $id => $field) {
    $field_name = $field['field'];
    if (isset($taken[$field_name])) {
      $field_name = $field['queryname'];
    }
    $taken[$field_name] = true;
    $field_names[$id] = $field_name;
  }

  // Set up some variables that won't change.
  $base_vars = array(
    'view' => $view,
    'view_type' => $type,
  );

  $items = array();
  foreach ($nodes as $i => $node) {
    $vars = $base_vars;
    $vars['node'] = $node;
    $vars['count'] = $i;
    $vars['stripe'] = $i % 2 ? 'even' : 'odd';
    foreach ($view->field as $id => $field) {
      $name = $field_names[$id];
      $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
      if (isset($field['label'])) {
        $vars[$name . '_label'] = $field['label'];
      }
    }
    $items[] = _phptemplate_callback('views-list-' . $template, $vars);
  }
  if ($items) {
    return theme('item_list', $items);
  }
}

/**
 * Create functions to catch views' check for theme overrides, and
 * pass on to a general function to call templates to output views.
 *
 * assumed faster than checking if a .tpl.php file exists for every view?
 */
function phptemplate_views_view_list_userpicbar(&$view, &$nodes, &$type) {
  return phptemplate_views_template('userpicbar', $view, $nodes, $type);
}
?>

Searched words: 
avoid redundant Views code in template.php view callback views phptemplate

Comments

Nat of OpenFlows replaces

Nat of OpenFlows replaces this:

<?php
  if ($items) {
    return theme('item_list', $items);
  }
?>

With a simple

<?php
  return $items;
?>

This gives him full access to wrapping the code in each list item template file, and from wherever you call the view you can put other wrappers. In particular, it means each themed item is not wrapped in <li> tags, and can be wrapped in custom divs or anything else.

For now I'm going to leave output as lists, argument being that this is semantic markup, and make Dan do the extra work of theming the lists to not display. But I'm thinking we can put an extra option in here to decide from the calling function whether it's a list or plain output. Nat also had a bit more code and logic in the function that called the "generic view" function, I can't recall what that might have done.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.