【问题标题】:Rails with HAML template Engine how label works in Haml?带有 HAML 模板引擎的 Rails 标签在 Haml 中如何工作?
【发布时间】:2013-09-04 04:09:34
【问题描述】:

我是 HAML 新手,无法理解旧的编码器逻辑 我尝试在 HAML 模板中识别标签在 ui 上的位置 这是我的代码 主视图页面

  = render :partial => "application/select_search", :locals => {:n => "benefit_stream_inf", :options => @dynamic_benefit_options, :default => true}

select_search 页面呈现在主平台

label = local_assigns[:l] ? l : t(n, :scope => local_assigns[:s] ? s : :models)

现在我没有在渲染中给出任何标签,那么标签是如何生成的

谁能解释一下这个标签分配代码

label = local_assigns[:l] ? l : t(n, :scope => local_assigns[:s] ? s : :models)

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 haml


    【解决方案1】:

    如果你看到下面的语句

    label = local_assigns[:l] ? l : t(n, :scope => local_assigns[:s] ? s : :models)
    

    使用的运算符是三元运算符

    表示标签可以是"l"t(n, :scope => local_assigns[:s] ? s : :models)

    看到这个

      = render :partial => "application/select_search", :locals => {:n => "benefit_stream_inf", :options => @dynamic_benefit_options, :default => true}
    

    我们可以说local_assigns[:l]会返回false意味着它会落入第二个选项,即t(n, :scope => local_assigns[:s] ? s : :models)

    t(n, :scope => local_assigns[:s] ? s : :models)

    在上面的语句n = "benefit_stream_inf"scope 中,我们在这个local_assigns[:s] 中有三元运算符,它将返回false 意味着最终label 变量将被分配给t("benefit_stream_inf", :scope => :models)

    如果您在项目中看到 en.yml models 哈希,其中包含 'benefit_stream_inf' key 和一些 value 此值将分配给标签变量,有关国际化的更多信息,您可以查看link

    我希望它会有所帮助。

    【讨论】:

      【解决方案2】:

      终于,我得到了答案

      label = local_assigns[:l] ? l : t(n, :scope => local_assigns[:s] ? s : :models)
      

      local_assigns[:l]是一个局部变量,用来直接给标签

      t(n, :scope => local_assigns[:s] ? s : :models) 这用于通过密钥和范围获取翻译文件的标签值

      翻译文件会放在 config/local/en.yml

      【讨论】:

      • 太好了,我打字的时候你得到了答案。
      猜你喜欢
      • 1970-01-01
      • 2011-05-05
      • 2012-06-09
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      相关资源
      最近更新 更多