# Next and Previous Articles

**URL:** <https://www.ruby-forum.com/t/next-and-previous-articles/66012>\
**Category:** Radiant CMS\
**Created:** [August 22, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/next-and-previous-articles/66012 "2006-08-22T21:49:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [August 22, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/next-and-previous-articles/66012/1 "2006-08-22T21:49:40Z")

</div>

Am I overlooking something, or as it currently stands is it  
impossible to create links the next article (next Child, in this  
case)? I need to create something like this for a small part of a  
site, which is basically done. This is the last thing holding me up…

Keith B.

---

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [August 22, 2006, 10:43pm UTC](https://www.ruby-forum.com/t/next-and-previous-articles/66012/2 "2006-08-22T22:43:51Z")

</div>

Keith B. wrote:

> Am I overlooking something, or as it currently stands is it  
> impossible to create links the next article (next Child, in this  
> case)? I need to create something like this for a small part of a  
> site, which is basically done. This is the last thing holding me up…

Something like this might work for you:

Behavior::Base.define\_tags do  
tag “next” do |tag|  
current = tag.locals.page  
siblings = current.siblings.sort\_by { |page| page.title }  
index = siblings.index(current)  
next = siblings[index + 1]  
if next  
tag.locals.page = next  
tag.expand  
end  
end

```
 tag "previous" do |tag|
   current = tag.locals.page
   by = tag.attr['by'] || 'title'
   siblings = current.siblings.sort_by { |page| page.attributes[by] 

```

}  
index = siblings.index(current)  
previous = siblings[index - 1]  
if previous  
tag.locals.page = previous  
tag.expand  
end  
end  
end

This would enable you to do something like this:

\<r:next:link /\>

Or even:

\<r:next by=“id”\>Next \>\>\</r:next\>

The above is completely untested.

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [August 22, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/next-and-previous-articles/66012/3 "2006-08-22T22:47:52Z")

</div>

John W. Long wrote:

> ```
> tag.expand
> end
> end
> 
> ```

The definition for next should be:

```
tag "next" do |tag|
  current = tag.locals.page
  by = tag.attr['by'] || 'title'
  siblings = current.siblings.sort_by { |page| page.attributes[by] }
  index = siblings.index(current)
  next = siblings[index + 1]
  if next
    tag.locals.page = next
    tag.expand
  end
end

```

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [August 23, 2006, 9:06am UTC](https://www.ruby-forum.com/t/next-and-previous-articles/66012/4 "2006-08-23T09:06:49Z")

</div>

John,

I am having a few problems with this. First of all, ‘next’ is a  
reserved word. Changed that to ‘next\_link’, but I am still getting a  
nil object for siblings. For some reason it is not finding any. This  
is with Radiant 0.5.2, any help appreiciated.

Keith B.

---

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [August 22, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/next-and-previous-articles/66012/5 "2006-08-22T23:39:57Z")

</div>

Thanks John, I will give that a try.

Keith

---

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [August 23, 2006, 9:50am UTC](https://www.ruby-forum.com/t/next-and-previous-articles/66012/6 "2006-08-23T09:50:52Z")

</div>

Ok, I think I have it here. Not as elegant as John’s, but for some  
reason current.siblings returns an empty array. I simply found the  
page’s parent and then the children, those are siblings, right? I am  
sure there is a better way to do this, but here is the code:

```
 tag "next_page" do |tag|
   current = tag.locals.page
   parent = tag.locals.page.parent
   by = tag.attr['by'] || 'title'
   siblings = parent.children.sort_by { |page| page.attributes[by] }
   index = siblings.index(current)
   next_page = siblings[index + 1]
   if next_page
     tag.locals.page = next_page
     tag.expand
   end
 end

 tag "prev_page" do |tag|
   current = tag.locals.page
   parent = tag.locals.page.parent
   by = tag.attr['by'] || 'title'
   siblings = parent.children.sort_by { |page| page.attributes[by] }
   index = siblings.index(current)
   prev_page = siblings[index - 1]
   if prev_page
     tag.locals.page = prev_page
     tag.expand
   end
 end

```

Still has a few problems, like showing the last link on the first  
page, but these are easily overcome.

Keith B.
