【问题标题】:How to conditionally use a "content_for" wrapper in Haml如何在 Haml 中有条件地使用“content_for”包装器
【发布时间】:2014-12-10 09:36:39
【问题描述】:

我正在尝试找到一种更干燥的方法来执行以下操作:

- if request.xhr?
  :javascript
    {my javascript}
- else
  = content_for :scripts do
    :javascript
      {my javascript}

我在许多 Haml 文件中重用了这种模式,所以我想知道抽象出条件逻辑的最佳方法是什么。理想情况下,我希望在我的 Haml 文件中实现以下目标:

optional_content_for :scripts do
  :javascript
    {my javascript}

【问题讨论】:

    标签: ruby-on-rails templates haml


    【解决方案1】:

    这似乎解决了我的需求,以防其他人有相同的要求......

    def script_content_for(&block)
      if request.xhr?
        capture_haml(&block)
      else
        content_for :scripts, nil, &block
      end
    end
    

    用途:

    = script_content_for do
      :javascript
        {my js}
    

    【讨论】:

    • 很好,我会把 :scripts 变成一个变量 def xhr_content_for(name, &block)
    猜你喜欢
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多