【问题标题】:Ruby Slim - How do you define an element's class with a rails helper or variable?Ruby Slim - 如何使用 rails 帮助器或变量定义元素的类?
【发布时间】:2012-10-12 03:35:40
【问题描述】:

在 rails slim (http://slim-lang.com/) 中,使用类名“sample”定义新 div 的语法如下:

 .sample
      = "Content goes here"

这将创建:

 <div class="sample">
      Content goes here
 </div>

我想根据rails的助手、变量或其他东西来定义一个div的类..例如,在rails中:

 <div class="sample #{@variable.name}">
   Content goes here
 </div>

我不知道如何在 slim 中做到这一点:

 .sample #what else goes here?
   Content goes here

有人知道怎么做吗?

【问题讨论】:

    标签: html css ruby-on-rails ruby-on-rails-3 slim-lang


    【解决方案1】:

    怎么样

    div[class="sample #{@variable.name}"]
    

    甚至

    div class=["sample", @variable.name]
    

    .sample *{:class => [@variable1.name, @variable2.name]}
    

    【讨论】:

    • 谢谢。这三个工作。第一个和第二个是最好的格式,但它们都起作用。再次感谢!
    • 另一个我更喜欢的选项:div class="sample #{@variable.name}"
    【解决方案2】:

    您可以使用括号、花括号或仅使用空格

    .first-class(class="second-class-#{ruby_call}")
    
    .first-class *{class: "second-class-#{ruby_call}"}
    
    .first-class class="second-class-#{ruby_call}"
    

    【讨论】:

      【解决方案3】:

      对于超薄模板,我一直使用括号表示法。您可以使用等号 ("=") 在同一行添加其他内容,但请务必包含空格:

      h1 class=("sample #{@variable.name}") = @variable.to_s
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-06
        • 2017-01-26
        • 2014-12-02
        • 1970-01-01
        • 2013-02-21
        • 2016-08-02
        • 2023-03-25
        • 1970-01-01
        相关资源
        最近更新 更多