Re: Next and Previous Articles

Keith B. wrote:

Not as elegant as John’s, but for some reason current.siblings
returns an empty array.

FWIW, the problem wasn’t that current.siblings was returning null.
Rather, the problem was that current.siblings returns a list that
doesn’t include the current page [1], so siblings.index(current) was
returning null. Calling “self_and_siblings” instead of “siblings”
works fine. Here’s the corrected version:

class Behavior::Base

define_tags do

 tag "next" do |tag|
   current = tag.locals.page
   by = tag.attr['by'] || 'title'
   siblings = current.self_and_siblings.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 "previous" do |tag|
   current = tag.locals.page
   by = tag.attr['by'] || 'title'
   siblings = current.self_and_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

end

  • Sean

[1] Peak Obsession
ClassMethods.html

On 06/09/06, Sean S. [email protected] wrote:

Here’s the corrected version:

If I want to use these behaviours, how do I go about that? :slight_smile:


Regards,
Dave

If I want to use these behaviours, how do I go about that? :slight_smile:

Oh, and if you were also wondering how you would actually use the
tags once you’ve got the behavior installed, here’s an example:

<r:previous by=“published_at”><r:link/></r:previous>
<r:next by=“published_at”><r:link/></r:next>

  • Sean

If I want to use these behaviours, how do I go about that? :slight_smile:

Create a file named “00_next_previous_behavior.rb”, paste in the
code, and save it to your radiant/app/behaviors directory. Then
restart the app.

HTH,

  • Sean

On 11/09/06, Sean S. [email protected] wrote:

Hi Sean,

Thanks for the quick response! :slight_smile:

If I want to use these behaviours, how do I go about that? :slight_smile:

Create a file named “00_next_previous_behavior.rb”, paste in the
code, and save it to your radiant/app/behaviors directory. Then
restart the app.

There was no radiant/app directory so I’ve created it and its
behaviors child. I now get these errors:

undefined tag previous' undefined tag next’

Could this work if it was packaged as a plugin?


Regards,
Dave

On 12/09/06, Sean S. [email protected] wrote:

There was no radiant/app directory so I’ve created it and its
behaviors child. I now get these errors:

Sorry, Dave, I should have been more clear. In your Radiant
installation (wherever that is), find the app directory, in which
there should be a behaviors directory. For example, if you installed
radiant in /home/dave/foo/radiant-0.5.2, you should put the
00_next_previous_behavior.rb in /home/dave/foo/radiant-0.5.2/app/
behaviors/.

Ah, another issue caused by using the gem installation then.

I’ll look into using the SVN version I think :slight_smile:

Thanks again!


Regards,
Dave

There was no radiant/app directory so I’ve created it and its
behaviors child. I now get these errors:

Sorry, Dave, I should have been more clear. In your Radiant
installation (wherever that is), find the app directory, in which
there should be a behaviors directory. For example, if you installed
radiant in /home/dave/foo/radiant-0.5.2, you should put the
00_next_previous_behavior.rb in /home/dave/foo/radiant-0.5.2/app/
behaviors/.

HTH,

  • Sean