1

我正在尝试使用 Haml + Mustache 创建博客,但“描述”字段具有 CKEditor,因此该字段始终包含 html 标签,但即使放置“description.html_safe”,Mustache 也不会呈现为 html。

我的助手:

def post_for_mustache(post)
{
  post: {
    category: {
      url: category_order_path(:category => post.category.to_param),
      name: post.category.name
    },
    url: category_post_path(:category => post.category.to_param,
                            :slug => post.to_param),
    title: post.title,
    comment_enabled: post.comment_enabled,
    description: post.description.html_safe,
    title_escape: URI.escape(post.title),
    url_escape: URI.escape(category_post_url(:category => post.category.to_param,
                                             :slug => post.to_param)),
  }
}
end

我的 Mustache 初始化程序:

module MustacheTemplateHandler
  def self.call(template)
    haml = "Haml::Engine.new(#{template.source.inspect}).render"
    if template.locals.include? 'mustache'
      "Mustache.render(#{haml}, mustache).html_safe"
    else
      haml.html_safe
    end
  end
end
ActionView::Template.register_template_handler(:mustache, MustacheTemplateHandler)
4

1 回答 1

4

我猜你在你的小胡子里做这样的事情:

{{description}}

如果description包含 HTML,则需要说:

{{{description}}}

来自精美手册

变量
[...]
默认情况下,所有变量都是 HTML 转义的。如果要返回未转义的 HTML,请使用三重小胡子: {{{name}}}

因此{{description}},将由 Mustache 编码 HTML,但{{{descriptipn}}}将按原样使用description

于 2012-08-02T18:51:38.790 回答