0

我有一个完整的配置文件......

   - if current_page.include? "test_string_one"
      - @total_index = 3
      - @next_location = '../random_string/page0.html'
      - @next_name = 'title 2'

    - if current_page.include? "test_string_two"
      - @total_index = 10
      - @next_location = '../another_random_string/page0.html'
      - @next_name = 'title 3'

有没有更干净的方法来写这个?使用静态

我看到haml中有可用的过滤器。所有这些都应该在:ruby过滤器中吗?

4

1 回答 1

1

这段代码最好是一个助手。

它可能看起来像这样:

module SomeHelper

  def page_options
    @page_options ||= begin
      options = {}

      if current_page.include? "test_string_one"
         options[:total_index] = 3
         options[:next_location] = '../random_string/page0.html'
         options[:next_name] = 'title 2'
      elsif current_page.include? "test_string_two"
         options[:total_index] = 10
         options[:next_location] = '../another_random_string/page0.html'
         options[:next_name] = 'title 3'
      end

      options
    end

  end

end

然后,在您需要的每个页面中,您可以访问如下选项:page_options[:total_index]

于 2009-11-16T18:08:04.533 回答