繁体   English   中英

Rails-如何将html属性添加到布局标签

[英]Rails - how to add an html attribute to a layout tag

这是我的应用程序布局页面的外观(这是其中的一部分)

%html
  %body
    #main
      .content
        = yield

有时(并非总是如此),我需要向.content div添加“ id”属性。 我该如何从html.haml页面上呢?

注意:我并不是要在运行时使用javascript做到这一点。

更新:谢谢,但是我忘了提到每个页面的“ id”是不同的。 换句话说,每个html.haml页面对于.content可能都有其自己的“ id”。

另一个解决方案:

%html
  %body
    #main
      .content{id: @content_id}
        = yield

#some .html.haml page
- @content_id = "my_id"

如果您未定义@content_id.content将没有id。

您可以根据需要随意设置菊花链ID和类

%html
  %body
    #main
      .content#random-id.random-class
        = yield

您可以执行以下操作,将id等属性添加到标签中:

.content{:id => "foo"}

您可以通过ruby哈希添加属性,并通过条件表达式设置值。

%html
  %body
    #main
      .content{:id => (id_needed ? 'my_id' : nil)}
        = yield

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM