# Listing first and last children

**URL:** <https://www.ruby-forum.com/t/listing-first-and-last-children/121072>\
**Category:** Radiant CMS\
**Created:** [November 23, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/listing-first-and-last-children/121072 "2007-11-23T14:02:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewneal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@andrewneal](https://www.ruby-forum.com/u/andrewneal)\
**Post date:** [November 23, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/listing-first-and-last-children/121072/1 "2007-11-23T14:02:24Z")

</div>

Hi,

On one of my sites, I’m using the following snippet:

\<r:children\>

\<r:first order=“desc”\>

  
\<r:snippet name=“entry”/\>  

  
\</r:first\>

\<r:each order=“desc” limit=“3” offset=“1”\>

  
\<r:snippet name=“entry”/\>  

  
\</r:each\>

\<r:last order=“desc” limit=“5”\>

  
\<r:snippet name=“entry”/\>  

  
\</r:last\>

\</r:children\>

The idea is that it should list the 5 most recent entries (child  
pages), wrapping each in a div.entry, except adding  
class=“firstentry” or “lastentry” to the first and last. I need these  
extra handles for the sake of CSS styling, which differs on first/  
last from middle entries.

The snippet above works fine, except when I have less than 5 child  
pages. In that case, the final entry is listed twice. This won’t be a  
problem in the future, when the site grows, but it is an irritation  
at this early stage where some of the pages calling this snippet have  
less than 5 pages. I wonder if anyone can suggest an improvement?

Thanks,

Drew

---

<div class="post-metadata">

**Author:** ![andrewneal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@andrewneal](https://www.ruby-forum.com/u/andrewneal)\
**Post date:** [November 30, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/listing-first-and-last-children/121072/2 "2007-11-30T13:31:56Z")

</div>

Maybe using something like:

\<r:children\>  
\<r:each order=“desc” limit=“1” offset=“0”\>  
…  
\</r:each\>  
\<r:each order=“desc” limit=“3” offset=“1”\>  
…  
\</r:each\>  
\<r:each order=“desc” limit=“1” offset=“4”\>  
…  
\</r:each\>  
\</r:children\>

```
In this case <r:each> should return an empty list in the last case.

/AITOR
```
