Hey guys, I'm pretty close to getting this one finished here..

I'm themeing a view inside of my template.php file like so:

function themename_views_view_list_view_name($view, $nodes, $type) {
  $fields = _views_get_fields();

  foreach ($nodes as $node) {
    $item = '';
    foreach ($view->field as $field) {
      $item .= views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
    }
    $items[] = "<div class=\"span-4\">$item</div>\n";
  }
  if ($items) {
    return implode("",$items);
  }
}

I'm calling the view inside of a Page node type like so:

$view = views_get_view('view_name');
print views_build_view('block', $view, array($title), false, 4);

I need to add "last" to the $items[] div on the fourth view item that is being called so that the generated code displays this:

<div class="span-4">VIEW ITEM</div>
<div class="span-4">VIEW ITEM</div>
<div class="span-4">VIEW ITEM</div>
<div class="span-4 last">VIEW ITEM</div>

I'm so close, but so far. Anyone have any ideas?

Thanks.

Comments

XBleed’s picture

Okay, well I seem to have it working now..

I kind of had to rig it to work, so my view theme function now looks like this:

function themename_views_view_list_view_name($view, $nodes, $type) {
  $fields = _views_get_fields();

  $i==0;
  foreach ($nodes as $node) {
    if($i==3) : 
      $class=' last'; 
      $i=0;
    else:
      $class='';
      $i=$i+1;
    endif;
    $item = '';
    foreach ($view->field as $field) {
      $item .= views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
    }
    $items[] = "<div class=\"span-4". $class ."\">$item</div>\n";
  }
  if ($items) {
    return implode("",$items);
  }
}

I imagine there's a more "Drupal" way to do this though. Anyone have a more efficient way of achieving this than what I have come up with?

Thanks.

zeta ζ’s picture

function themename_views_view_list_view_name($view, $nodes, $type) {
  $fields = _views_get_fields();
  $num_items = count($items);

  $i = 0;
  foreach ($nodes as $node) {
    $i++;
    $item = '';
    foreach ($view->field as $field) {
      $item .= views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
    }
    $class = 'span-4';
    if ($i == 0) {
      $class .= ' first';
    }
    if ($i == $num_items - 1) {
      $class .= ' last';
    }
    $items[] = "<div class=\"$class\">$item</div>\n";
  }
  if ($items) {
    return implode("",$items);
  }
}

___________________
It’s in the detaιls…

demonstration portfolio

h_dries’s picture

is there a way to let this work together with a tpl.php file,
so you can add divs for the 'general' output in the tpl.php file
and specific divs through the function.

in other words get this functionality combined:

	$items[] = _phptemplate_callback('views-list-accordion', $vars);
	$items[] .= "<div class=\"$class\">$item</div>\n";
zeta ζ’s picture

What have you tried?
What are the symptoms?

I think that;-

 $items[] = ...;
$items[] .= ...; 

is not going to do what you expect!
___________________
It’s in the detaιls…

demonstration portfolio

zeta ζ’s picture

function themename_views_view_list_view_name($view, $nodes, $type) {
  $fields = _views_get_fields();
  $num_items = count($items);
  $span = "span-$num_items";

  $i = 0;
  foreach ($nodes as $node) {
    $i++;
    $item = '';
    foreach ($view->field as $field) {
      $item .= views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
    }
    $class = $span;
    if ($i == 0) {
      $class .= ' first';
    }
    if ($i == $num_items - 1) {
      $class .= ' last';
    }
    $items[] = "<div class=\"$class\">$item</div>\n";
  }
  if ($items) {
    return implode("",$items);
  }
}

___________________
It’s in the detaιls…

demonstration portfolio

h_dries’s picture

Thanks for the reply, but that's not what I meant:
If I wanted a general wrap around the node, I would just add it in a tpl.php file.
I want my nodes come out with the divs I added in tpl.php and an unique wrap div around them.

say:
div id="node1"
div class="title"
div class="body"

div id="node2"
div class="title"
div class="body"

div id="node3"
div class="title"
div class="body"

div id="node4"
div class="title"
div class="body"

Makes sense now? title and body are added in tpl.php, the node-number are added through template.php

zeta ζ’s picture

Have you replied to the correct comment? My comment Generalise span # is a reply to my previous comment Try this....

Then again, I’m not sure how this relates to together with tpl.php file ? either.

If you want <div id="node1">...</div>, you can put <div id="node<?php print $id; ?>">...</div> in node.tpl.php.
___________________
It’s in the detaιls…

demonstration portfolio