簡體   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