【问题标题】:Haml + Mustache哈姆 + 小胡子
【发布时间】:2012-08-02 18:16:14
【问题描述】:

我正在尝试使用 Haml + Mustache 创建一个博客,但“description”字段有一个 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)

【问题讨论】:

    标签: ruby-on-rails ruby haml mustache


    【解决方案1】:

    我猜你的胡子是这样的:

    {{description}}
    

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

    {{{description}}}
    

    来自fine manual

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

    所以{{description}} 将是由 Mustache 编码的 HTML,但 {{{descriptipn}}} 将按原样使用 description

    【讨论】:

      猜你喜欢
      • 2012-01-03
      • 2011-08-29
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2012-04-18
      相关资源
      最近更新 更多