Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including contents of a variable inside a Haml filter

How to make article.content filtered by :textile ?

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

the output

<article class="post">
    <header>game</header>
    <p>
      </p><p>=article.content</p>
    <p></p>
    <footer></footer>
  </article>
like image 947
wizztjh Avatar asked Apr 06 '11 11:04

wizztjh


2 Answers

Inside a Haml filter you use String interpolation to include Ruby code in your markup. For example:

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"

Edit: My previous answer was misleading with respect to newlines in the content, due to my flawed testing. As seen above, newlines in included content are handled fine directly.

As such, your Haml template should simply look like this:

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

Note that I've removed your %p tag surrounding your markup, as Textile introduces its own paragraph wrappers (even for single-line content).

like image 64
Phrogz Avatar answered Nov 20 '22 02:11

Phrogz


Your syntax seems fine, if everything else is getting rendered properly maybe it's a gem issue. Do you have RedCloth installed?

Edit: On second thought, what does the second line do? It might be the cause of your problem since I don't think %article.post is proper HAML syntax and the :textile filter is inside it

like image 1
Quasar Avatar answered Nov 20 '22 02:11

Quasar