2009-11-16 17 views
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' 

有沒有寫這個的更清潔的方式?使用Staticmatic

我看到haml中有可用的過濾器。這一切應該在:ruby過濾器?

+0

Dup的http://stackoverflow.com/questions/1743291/haml-syntax-better-way-of-writing-this – 2009-11-16 18:11:23

回答

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]

+0

似乎會出現一個錯誤 - 我已經將htis文件保存爲page_helper.rb在助手中 - staticmatic/mixins/helpers.rb:13:in'load_helper ':(eval):1:in'load_helper':未初始化的常量Haml :: Helpers :: PageHelper(NameError) – 2009-11-16 18:38:08

+0

這是因爲SomeHelper應該被保存爲some_helper.rb in hel個人 – 2009-11-16 20:10:43