简体   繁体   English

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

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

This is how my application layout page is looking (here is a part of it) 这是我的应用程序布局页面的外观(这是其中的一部分)

%html
  %body
    #main
      .content
        = yield

Sometimes (not always) I need to add "id" attribute to .content div . 有时(并非总是如此),我需要向.content div添加“ id”属性。 How can I do it from a html.haml page? 我该如何从html.haml页面上呢?

Note: I don't mean to do it in runtime using javascript. 注意:我并不是要在运行时使用javascript做到这一点。

UPDATE: thanks, but I forgot to mention that this "id" is different for each page. 更新:谢谢,但是我忘了提到每个页面的“ id”是不同的。 In other words, each html.haml page might have its own "id" for .content . 换句话说,每个html.haml页面对于.content可能都有其自己的“ id”。

And another solution: 另一个解决方案:

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

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

If you don't define @content_id then .content would be without id. 如果您未定义@content_id.content将没有id。

You can just daisy chain ids and classes as much as you want 您可以根据需要随意设置菊花链ID和类

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

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

.content{:id => "foo"}

You can add attributes via ruby a Hash and set the value through a conditional expression. 您可以通过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