在Haml过滤器中包含变量的内容

wiz*_*tjh 7 haml ruby-on-rails

如何article.content过滤:纺织?

 - @articles.each do |article|
    %article.post
      %header=article.name
      %p
        :textile 
          =article.content
      %footer
Run Code Online (Sandbox Code Playgroud)

输出

<article class="post">
    <header>game</header>
    <p>
      </p><p>=article.content</p>
    <p></p>
    <footer></footer>
  </article>
Run Code Online (Sandbox Code Playgroud)

Phr*_*ogz 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"
Run Code Online (Sandbox Code Playgroud)

编辑:由于我的测试存在缺陷,我之前的回答误导了内容中的换行符.如上所示,包含内容中的换行符可以直接处理.

因此,您的Haml模板应该如下所示:

- @articles.each do |article|
  %article.post
    %header=article.name
    :textile 
      #{article.content}
    %footer
Run Code Online (Sandbox Code Playgroud)

请注意,我已经删除了%p围绕标记的标记,因为Textile引入了自己的段落包装(即使对于单行内容).