1

I only want to output one anchor here. If the current_page is in the array I get two (.html and -nf.html). If it is not in the array I get as many anchors as there are items in the array.

I am using StaticMatic.

- if page_options[:current_index] < page_options[:total_index] && page_options[:current_index] > 0

    // loop through the pre-flash array and if current page matches, add -nf
    - page_options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf']
    - page_options[:pre_flash].each do |each_string|

        - if current_page.include? each_string
            %li 
                %a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" }
                    Next
        - else
            %li
                %a{ :href => "page#{page_options[:current_index]}.html", :class => "next" }
                    Next
Dr. Frankenstein
  • 4,634
  • 7
  • 33
  • 48

2 Answers2

2
unless current_page.include? the_string

Edit:

You could break the each-loop if you want your first finding to be the only one. But now this looks a little bit weird, because you are iterating over an array and breaking after the first element no matter what happens. Am I addressing your problem at all?

options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf']
page_options[:pre_flash].each do |each_string|
  if current_page.include? each_string
    %li 
    %a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" }
    # This is the last ancor
    break
  else
    %li 
    %a{ :href => "page#{page_options[:current_index]}.html", :class => "next" }
    # This is the last ancor
    break
  end
end
Steinbitglis
  • 2,482
  • 2
  • 27
  • 40
  • sorry guys - there is more to this than i realised, simply doing what u reccomend, i get multiple occurences of the unless or ! code (in this case the anchor) – Dr. Frankenstein Dec 01 '09 at 00:40
1

Okay, so I think we're checking that none of page_options[:current_index] are substrings of current_page.

if page_options[:current_index] < page_options[:total_index] && page_options[:current_index] > 0

found_item = false

// loop through the pre-flash array and if current page matches, add -nf
- page_options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf']
- page_options[:pre_flash].each do |each_string|

    - if current_page.include? each_string
            found_item = true
            %li 
                    %a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" }
                            Next

# do whatever you need to get out of the staticmatic block...

    - if !found_item

            %li
                    %a{ :href => "page#{page_options[:current_index]}.html", :class => "next" }

Sorry - I misunderstood what you were doing... thought you were doing an include? on an array but it was a string... :-)

Dafydd Rees
  • 6,941
  • 3
  • 39
  • 48
  • this sounds along the right lines though http://pastie.org/721159 is giving me a 'can't convert String into Array' error – Dr. Frankenstein Dec 01 '09 at 01:03
  • The pastie isn't quite what I meant - put the second if outside the block... Incidentally - what type is current_page? is it a String? – Dafydd Rees Dec 01 '09 at 01:08
  • current_page is indeed a string – Dr. Frankenstein Dec 01 '09 at 01:13
  • Putting this inside the each, which iterates and delivers each_string, and do something not related to each_string seems wrong in my mind... What am I missing here? – Steinbitglis Dec 01 '09 at 01:29
  • 1
    The only thing I'm adding is a flag, `found_item` that gets set if any of the items in page_options[:pre_flash] are substrings of `current_page`. Both uses of the block are related because they operate on the same collection - but one use is setting the flag and the other is display - so it's a moot point whether you should do this in a separate block... – Dafydd Rees Dec 01 '09 at 01:37
  • I am totally into what you are doing here and I feel better doing this than using break. However, I've tried to get your code to work with no joy. I'll have another bash another day but unfortunately the big tick is going to Styggentorsken. Thanks a lot - I've learnt a lot from this. – Dr. Frankenstein Dec 01 '09 at 01:49