[Radiant] Next and Previous Articles

John W. Long ng at johnwlong.com
Tue Aug 22 15:43:04 CDT 2006


Keith Bingman 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"><a href="<r:url />">Next >></a></r:next>

The above is completely untested.

--
John Long
http://wiseheartdesign.com



More information about the Radiant mailing list