简体   繁体   中英

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 . How can I do it from a html.haml page?

Note: I don't mean to do it in runtime using javascript.

UPDATE: thanks, but I forgot to mention that this "id" is different for each page. In other words, each html.haml page might have its own "id" for .content .

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.

You can just daisy chain ids and classes as much as you want

%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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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