<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>slaug's blog</title>
  <link rel="alternate" type="text/html" href="http://pingv.com/blog/870"/>
  <link rel="self" type="application/atom+xml" href="http://pingv.com/blog/870/atom/feed"/>
  <id>http://pingv.com/blog/870/atom/feed</id>
  <updated>2008-02-22T11:07:36-06:00</updated>
  <entry>
    <title>Combining different views using views offsets</title>
    <link rel="alternate" type="text/html" href="http://pingv.com/blog/slaug/200802/view-offsets" />
    <id>http://pingv.com/blog/slaug/200802/view-offsets</id>
    <published>2008-02-22T11:07:36-06:00</published>
    <updated>2008-02-22T11:07:36-06:00</updated>
    <author>
      <name>slaug</name>
    </author>
    <category term="code" />
    <category term="Drupal" />
    <category term="Views" />
    <content type="html"><![CDATA[ <p>We recently had a client that wanted to have two different teaser treatments for a node type on their homepage: The first node would show more content, while the other nodes would have less content displayed. This is common for displays where you want, for example, a highlighted top post with more abbreviated lower posts. </p>
<p>We approached this using two views. The top view would return the top node of the view, while the second view would return everything but the most recent item, while displaying different teasers. The first view was easy: simply the most recent node of that type that had been published.</p>
<p>The second view, however, was created by using two functions from the views module and placed in the <code>page-front.tpl.php</code> file:</p>
<ol>
<li><code>views_get_view($view_name)</code></li>
<li><code>views_build_view($type, &amp;$view, $args = array(), $use_pager = false, $limit = 0, $page = 0, $offset = 0, $filters = NULL)</code></li>
</ol>
<p>We used  <code>views_get_view()</code> to get the view object for the view and set it to a variable:</p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$second </span><span style="color: #007700">= </span><span style="color: #0000BB">views_get_view</span><span style="color: #007700">(</span><span style="color: #DD0000">'Secondary_topics'</span><span style="color: #007700">); <br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p>Next we used the <code>views_build_view()</code> to display the view on the home page:</p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">views_build_view</span><span style="color: #007700">(</span><span style="color: #DD0000">'embed'</span><span style="color: #007700">, </span><span style="color: #0000BB">$second</span><span style="color: #007700">, array(), </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p>We stated the type as <code>embed</code> because the view is being embedded in a page. The the next parameter, <code>$second</code>, is the views object, which was returned by the <code>views_get_view</code> function.</p>
<p>To achieve the goal of getting five teaser views starting at the second most recent topic, we set the <code>$limit</code> parameter to 5 and the <code>$offset</code> parameter to 1. </p>
<p>For the other view that only displays the most recent node topic we used the same two functions, but set the <code>$limit</code> parameter to 1 in the <code>views_build_view</code> function:</p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$first </span><span style="color: #007700">= </span><span style="color: #0000BB">views_get_view</span><span style="color: #007700">(</span><span style="color: #DD0000">'Front_Page'</span><span style="color: #007700">);<br />print </span><span style="color: #0000BB">views_build_view</span><span style="color: #007700">(</span><span style="color: #DD0000">'embed'</span><span style="color: #007700">, </span><span style="color: #0000BB">$first</span><span style="color: #007700">, array(), </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p>We placed both of these steps into the <code>page-front.tpl.php</code> template in the appropriate places to display the content on the page and in the order that is needed.  </p>
<p>Now the new issue is this: both contents look the same because the views are both calling the same <code>node.tpl.php</code> file and there is no flag to check for which view is being used.  </p>
<p>To resolve this issue we used the global variable <code>$current_view</code> as a flag. The <code>$current_view</code> variable is an object that contains the name of the view being used (<code>$current_view-&gt;name</code>). We placed the <code>$current_view</code> variable at the top of the <code>node.tpl.php</code> template file.  Next we used a conditional statement (if..elseif..else) to have the <code>node.tpl.php</code> file display different content for each view on the same page.   </p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /> </span><span style="color: #007700">global </span><span style="color: #0000BB">$current_view</span><span style="color: #007700">; <br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p>This displays content for the first topic view on the front page, but not for the other view on the front page:</p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">if((</span><span style="color: #0000BB">$current_view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name </span><span style="color: #007700">== </span><span style="color: #DD0000">'Front_Page'</span><span style="color: #007700">) &amp;&amp; </span><span style="color: #0000BB">$is_front</span><span style="color: #007700">): </span><span style="color: #0000BB">?&gt;</span></span><br />This will only display in the first view on the front page.<br /><span style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">endif; </span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p>I hope this helps other Drupal users. If anyone else has come across this and did it a different way, please share.</p>
     ]]></content>
  </entry>
</feed>
