【问题标题】:Including contents of a variable inside a Haml filter在 Haml 过滤器中包含变量的内容
【发布时间】:2011-04-06 11:09:50
【问题描述】:

如何让article.content被 :textile 过滤?

 - @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>

【问题讨论】:

    标签: ruby-on-rails haml


    【解决方案1】:

    在 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 标签,因为 Textile 引入了自己的段落包装器(即使是单行内容)。

    【讨论】:

    • @wizztjh 我的回答错了;请参阅上面的更新示例,表明它应该“正常工作”。
    【解决方案2】:

    您的语法看起来不错,如果其他所有内容都正确呈现,则可能是 gem 问题。你有安装 RedCloth 吗?

    编辑:再想一想,第二行是做什么的?这可能是您的问题的原因,因为我认为 %article.post 不是正确的 HAML 语法,并且 :textile 过滤器在其中

    【讨论】:

    • 我认为%article.post 生成&lt;article class="post"&gt;
    猜你喜欢
    • 2014-01-19
    • 2011-06-08
    • 2012-06-10
    • 1970-01-01
    • 2016-06-21
    • 2018-02-12
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多