2011-04-06 27 views
7

如何使article.content過濾方式:textile?在Haml過濾器中包含變量的內容

- @articles.each do |article| 
    %article.post 
     %header=article.name 
     %p 
     :textile 
      =article.content 
     %footer 

輸出

<article class="post"> 
    <header>game</header> 
    <p> 
     </p><p>=article.content</p> 
    <p></p> 
    <footer></footer> 
    </article> 

回答

7

在Haml過濾器中,您使用字符串插值在您的標記中包含Ruby代碼。例如:

require 'haml'        
@x = 42            
Haml::Engine.new("%p= @x").render(self)    #=> "<p>42</p>\n" 
Haml::Engine.new(":textile\n\t= @x").render(self) #=> "<p>= @x</p>\n" 
Haml::Engine.new(":textile\n\t\#{@x}").render(self) #=> "<p>42</p>\n" 

@content = "alpha\n\n#hi **mom**" 
Haml::Engine.new(":textile\n\t\#{@content}").render(self) 
#=> "<p>alpha</p>\n<p>#hi <b>mom</b></p>\n" 

編輯:我以前的答案就在內容換行,誤導由於我的缺陷檢測。如上所示,包含內容中的換行符可以直接處理。

因此,您Haml的模板應該簡單地看是這樣的:

- @articles.each do |article| 
    %article.post 
    %header=article.name 
    :textile 
     #{article.content} 
    %footer 

請注意,我已經刪除您%p標籤周圍您的標記,如紡織推出了自己的一款包裝(即使是單線內容)。

+0

@wizztjh我的回答錯了;請參閱上面的更新示例,以顯示它應該「只是工作」,只要你喜歡。 – Phrogz 2011-04-06 15:56:58

1

你的語法似乎罰款,如果其它所有事情都變得越來越正常,也許這是一個寶石的問題。你有RedCloth安裝?

編輯:關於第二個想法,第二行是幹什麼的?這可能是你的問題的原因,因爲我不認爲%article.post是正確的HAML語法,並且:紡織過濾器在它內部

+0

我已經安裝了redcloth,讓我顯示錯誤 – wizztjh 2011-04-06 14:20:39

+0

我認爲'%article.post'生成'

+0

是的,我注意到,我的不好。 – Quasar 2011-04-07 10:37:52