Next and Previous Articles

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.

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

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

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.

Thanks John, I will give that a try.

Keith

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.