Combining different views using views offsets

Comments

manuee writes:

Very interesting post, I remember wondering how to go about doing this not so long ago... your article triggered me to have a go at it. Here is how i did it, based on the handy information you provided:

Created two views, only filtering published nodes, and sorting on created time (descending). Only difference between them is one is a teaser view (secondary_nodes), and the other is a full nodes view (latest_node).

I then put this code instead of the $content on my page-front.tpl.php:

<?php
    $first 
= views_get_view('latest_node');
    print
views_build_view('embed', $first, array(), FALSE, 1);
   
$second = views_get_view('secondary_nodes');
    print
views_build_view('embed', $second, array(), FALSE, 10, 0, 1);
?>

Seems to work great, many thanks for sharing! :D

Elvis McNeely writes:

Thanks for sharing this. I am curious if there is anything special to do in the view itself?

In the past I have done something similar by creating functions in template.php then calling those functions from a block:

<?php print function_name($type='blog' $max='5'); ?>

I imagine you could do the same this with the views_build_view() function?

Jody Hamilton writes:

This is interesting, especially the global $current->view.

I believe if you bring in your views as panel views into Panels 2 that you can actually set an offset when you do that and get the same effect.

I also think it may be possible to include an offset in the 'argument handling code' section of the view, perhaps by just entering $view->offset = 1; ? Not sure about that, but I know you can actually do a lot of custom things to views by putting code there.

merlinofchaos writes:

I have done similar treatments with Views using a purely theming method. There are a few really interesting tricks you can do because theme_views_view_nodes() is so very simple.

Assuming my view is named 'foobar':

<?php
/**
* Display the nodes of a view as plain nodes.
*/
function phptemplate_views_view_teasers_foobar($view, $nodes, $type) {
  foreach (
$nodes as $count => $n) {
   
$node = node_load($n->nid);
   
$node->template = $count ? 'short' : 'long'; // apply a variable to the node that we can use later.
   
$output .= node_view($node, TRUE, FALSE, TRUE);
  }
  return
$output;
}
?>

In template.php, in _phptemplate_variables, under case 'node':

<?php
 
case 'node':
    if (!empty(
$vars['node']->template)) {
     
$vars['template_files'][] = 'node-view-' . $vars['node']->template;
    }
?>

Now, based upon the value I set in the view theme function, the actual node template used is based upon whether count is 0 (node-views-long.tpl.php) or non-zier (node-views-short.tpl.php).

This method is superior because 1) only one view 2) purely at the theming layer 3) less code 4) very very flexible.

gollyg writes:

This is the same approach I have taken, and it seems to work perfectly. There is no issue of 'normalising' you views, as you are only dealing with the one view.

dalin writes:

global $current_view; won't always work for you. It can give false positives if the view exists in a block and you're logic switching is in node.tpl.php

I think there's a situation where it can give you false negatives too, but I can't remember what that is.

dagomar writes:

Hi,

it seems to me that this is more work than needed! I made for my girlfriend a blog and on the homepage it will show the latest post (a view that takes the latest post and displays it completely), then i created a second -list- block - view that shows only the titles as link with an offset in the argument field of that view ($offset = 1;). There was no need for me to fiddle around with template files! Just 1 view and a views block!