2013-07-08 74 views
8

我正在尋找一種將樣式錶鏈接添加到HAML模板的佈局頭部的方法。如何將樣式表添加到Sinatra的HAML模板的佈局頭部?

我的佈局:

!!! 
    %html 
    %head 
    /some stuffs 
    %body 
     = yield 

我得到的模板:

/ some other stuffs... 

/maybe a function like this in order to inject 'my_stylesheet' link in layout 
= content_for_head 'my_stylesheet' 

是否有可能做這樣的事情?

+2

將它添加到你的頭上:'%鏈接HREF =「/路徑/到/ stylesheet.css中」 rel =「stylesheet」 –

+0

這個東西是我不希望共享這個佈局的所有視圖都加載了這個樣式表。 – szymanowski

+0

如何確定哪些視圖獲取樣式表,哪些視圖不能獲取樣式表?有沒有一種模式? –

回答

13

有兩種方法可以解決它。一種是使用Sinatra自己的content_for gem,或捆綁ActionView,它可以讓你訪問Rails的content_for方法。

第二個選擇是做佈局手動檢查,包括那裏的CSS:

# in your HAML template: 
- if request.path_info == '/hello-world' 
    %link{:rel => :stylesheet, :type => :"text/css", :href => "/assets/css/my_stylesheet"} 
相關問題