I've been reading the handbook and forums all afternoon working on trying to get my views, using the views module, to be displayed differently. I'm a little lost as to where to put the code to affect the change.

I created my own theme and in that theme folder I have a template.php file. Do I put a function in that file or does it go someplace else?

While researching I saw this code and see the CSS for layout display, but I can't find how I change the classes such as div class='view-label view-label-$field[queryname]' to change the format for my item and fields. Do I add a CSS style to my style sheet that's in my themes folder? If so, what do I name it?

I only need to show the title and the start time for an event, but it shows up on 2 lines with the title indented and the field outdented. I would like either one line or the opposite - title outdented, field indented.

THANKS for the help!

/**
* Display the nodes of a view as a list.
*/
function theme_views_view_list($view, $nodes) {
  $fields = _views_get_fields();

  foreach ($nodes as $node) {
    $item = '';
    foreach ($view->field as $field) {
      if ($field['label']) {
        $item .= "<div class='view-label view-label-$field[queryname]'>" . $field['label'] . "</div>";
      }
      $item .= "<div class='view-field view-data-$field[queryname]'>" . views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node) . "</div>";
    }
    $items[] = "<div class='view-item view-item-$view->name'>$item</div>\n"; // l($node->title, "node/$node->nid");
  }
  if ($items) {
    return theme('item_list', $items);
  }
}

Comments

merlinofchaos’s picture

Yea, this is a part of views theming that I have a plan to write a helper tool to get people started. I think you can do what you want using just CSS, but you could also override the above function.

1) Put the function in your template.php -- put any CSS in your style.css
2) name the function phptemplate_views_view_list_VIEWNAME
3) the CSS is pretty easy to work with as is. Look at your view output's source (or use something like Firefox's webdeveloper extension -- the ctrl-shift-F option makes it very easy to find class/id info on your page).

With just CSS you should be able to change the margin and padding on the items; you can also make them inline if you need.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

cschall’s picture

I apologize for not quite getting the obvious.

This is what I have in the code that displays my information:

<div class="item-list"><ul><li><div class='view-item view-item-namehere'><div class='view-field view-data-node_title'><a href="/?q=node/28">Event Information</a></div><div class='view-field view-data-event_event_start'>Jun 19 2006 - 10:30am</div></div>

So do I create a class that's actually named 'view-item view-item-namehere' and a second one that's named view-field view-data-node_title' and a third that's named 'view-field view-data-event_event_start'? (no quotes)

Then do I name my function phptemplate_views_view_list_VIEWNAME and replace "VIEWNAME" with "namehere"?

Thanks!!!

cschall’s picture

I created a function and added it to template.php, but I think that I missing something because I'm not sure how to tell my blocks to use the function that I created.

styro’s picture

So do I create a class that's actually named 'view-item view-item-namehere' and a second one that's named view-field view-data-node_title' and a third that's named 'view-field view-data-event_event_start'?

CSS class names can't have spaces in them. The space is used to separate multiple classes. An HTML element can have multiple classes attached to it.

So the available classes for styling are:

(general ones)
view-item
view-field

(more specific ones)
view-item-namehere
view-data-node_title
view-data-event_event_start

The general/specific split (just like with blocks) allows you to either style a whole bunch of things or just one specific type of thing.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
Example Knowledge Base built using Drupal

cklester’s picture

The following should put a little surprise (BOO!) into the HTML, shouldn't it? But I'm not seeing it on the final page.

Heeellllllpppp!!!! :)

/**
* Display the nodes of a view as a list.
*/
function phptemplate_theme_views_view_list_All_Articles($view, $nodes) {
  $fields = _views_get_fields();

  foreach ($nodes as $node) {
    $item = '';
    foreach ($view->field as $field) {
      if ($field['label']) {
        $item .= "<div class='view-label view-label-$field[queryname]'>" . $field['label'] . "</div>";
      }
      $item .= "<p>BOO!</p><div class='view-field view-data-$field[queryname]'>" . views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node) . "</div>";
    }
    $items[] = "<div class='view-item view-item-$view->name'>$item</div>\n"; // l($node->title, "node/$node->nid");
  }
  if ($items) {
    return theme('item_list', $items);
  }
}
merlinofchaos’s picture

phptemplate_theme_views_view_list_All_Articles

Should be

phptemplate_views_view_list_All_Articles

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

cklester’s picture

That would be nice if that's the problem. Nice and simple and an easy lesson to learn! :)

Thank you! :)