【问题标题】:haml syntax - better way of writing this?haml 语法 - 更好的写法?
【发布时间】:2009-11-16 18:01:39
【问题描述】:

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

   - 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 过滤器中吗?

【问题讨论】:

标签: ruby haml staticmatic


【解决方案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]

【讨论】:

  • 似乎出现了错误 - 我已将 htis 文件保存为帮助程序中的 page_helper.rb - staticmatic/mixins/helpers.rb:13:in load_helper': (eval):1:in load_helper': 未初始化的常量 Haml ::Helpers::PageHelper (NameError)
  • 那是因为 SomeHelper 应该在 helpers 中保存为 some_helper.rb
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
  • 2014-10-21
  • 2018-08-01
  • 2015-07-19
  • 1970-01-01
相关资源
最近更新 更多