[Radiant] Next and Previous Articles

Keith Bingman keith at keithbingman.com
Wed Aug 23 02:48:56 CDT 2006


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 Bingman

On Aug 23, 2006, at 9:04 AM, Keith Bingman wrote:

> 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 Bingman
>
> On Aug 22, 2006, at 10:46 PM, John W. Long wrote:
>
>>     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
>
> _______________________________________________
> Radiant mailing list
> Radiant at lists.radiantcms.org
> http://lists.radiantcms.org/mailman/listinfo/radiant

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.radiantcms.org/pipermail/radiant/attachments/20060823/549eafd8/attachment.html


More information about the Radiant mailing list