Frank Ellebrecht
2012-Jun-24 19:45 UTC
[webgen-users] How to use only some nodes of an erb hash?
Hello, I am trying to adapt the blog extension by Matteo Collina and Damien Pollet to my needs. After a lot of trial and error most of it works. Now I am trying to get some parts from the blog pages to show up in the sidebar, using some code by Damien Pollet [1]: <% pages = context.content_node.tree.node_access[:alcn].select{ |alcn,no| alcn =~ /\d{4}\/.*\.blog/ && no.is_file? } pages = pages.collect{ |na,no| no}.sort{ |a,b| b[''date''] <=> a[''date''] } pages.each do |node| %> ... display stuff from the entry? <% end %> It works, but I wonder if there is any way to restrict the number of shown nodes, e.g. the titles of the three most recent ones? I''m not a programmer and didn''t have any former knowledge of ruby or erb, but I think I have to replace the each-do-loop with something different---but what? Can someone please point me in the right direction? Kind regards, Frank [1] <http://rubyforge.org/pipermail/webgen-users/2009-November/000581.html>
Thomas Leitner
2012-Jun-25 09:03 UTC
[webgen-users] How to use only some nodes of an erb hash?
On 2012-06-24 21:45 +0200 Frank Ellebrecht wrote:> <% > pages = context.content_node.tree.node_access[:alcn].select{ > |alcn,no| alcn =~ /\d{4}\/.*\.blog/ && no.is_file? } > pages = pages.collect{ |na,no| no}.sort{ |a,b| b[''date''] <=> > a[''date''] } pages.each do |node| > %> > ... display stuff from the entry? > <% end %> > > It works, but I wonder if there is any way to restrict the number of > shown nodes, e.g. the titles of the three most recent ones? I''m not a > programmer and didn''t have any former knowledge of ruby or erb, but I > think I have to replace the each-do-loop with something > different---but what? Can someone please point me in the right > direction?Instead of `pages.each do |node|` use either * `pages[0...3].each do |node|` for the first three pages or * `pages[-3..-1].each do |node|` for the last three pages Best regards, Thomas
Frank Ellebrecht
2012-Jun-25 16:35 UTC
[webgen-users] How to use only some nodes of an erb hash?
Hi Thomas, It works! Thanks a lot. More questions still to come ... Kind regards, Frank Am 25.06.2012 11:03, schrieb Thomas Leitner:> On 2012-06-24 21:45 +0200 Frank Ellebrecht wrote: >> <% >> pages = context.content_node.tree.node_access[:alcn].select{ >> |alcn,no| alcn =~ /\d{4}\/.*\.blog/ && no.is_file? } >> pages = pages.collect{ |na,no| no}.sort{ |a,b| b[''date''] <=> >> a[''date''] } pages.each do |node| >> %> >> ... display stuff from the entry? >> <% end %> >> >> It works, but I wonder if there is any way to restrict the number of >> shown nodes, e.g. the titles of the three most recent ones? I''m not a >> programmer and didn''t have any former knowledge of ruby or erb, but I >> think I have to replace the each-do-loop with something >> different---but what? Can someone please point me in the right >> direction? > > Instead of `pages.each do |node|` use either > > * `pages[0...3].each do |node|` for the first three pages or > * `pages[-3..-1].each do |node|` for the last three pages > > Best regards, > Thomas