Alexandre
Alexandre

Reputation: 13318

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.

Upvotes: 0

Views: 673

Answers (4)

Mik
Mik

Reputation: 4187

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.

Upvotes: 1

unnu
unnu

Reputation: 754

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

Upvotes: 0

Andrew Kuklewicz
Andrew Kuklewicz

Reputation: 10701

You can do this to add attributes like id to a tag:

.content{:id => "foo"}

Upvotes: 0

Jason Kim
Jason Kim

Reputation: 19061

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

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

Upvotes: 0

Related Questions