2009-11-16 5 views
0

나는이 가득 설정 파일을 가지고 .... haml 구문 - 이것을 작성하는 더 좋은 방법은 무엇입니까?

- 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- –

답변

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

오류가 떠오를 것 같아요. 헬퍼에 page_helper.rb라는 파일을 저장했습니다. -staticmatic/mixins/helpers.rb : 13 : load_helper ': (eval) : 1 :'load_helper': 초기화되지 않은 상수 Haml :: Helpers :: PageHelper (NameError) –

+0

SomeHelper가 hel의 some_helper.rb로 저장되어야하기 때문입니다. 인사 –

관련 문제