代码之家  ›  专栏  ›  技术社区  ›  Matheus Caceres

火腿+芥末

  •  1
  • Matheus Caceres  · 技术社区  · 14 年前

    我正试图用Haml+Mustache创建一个博客,但字段“description”有一个CKEditor,因此该字段总是包含html标记,但Mustache即使放入“description.html_safe”也不会呈现为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)
    
    1 回复  |  直到 14 年前
        1
  •  4
  •   mu is too short    14 年前

    我猜你在你的野马车上做了这样的事情:

    {{description}}
    

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

    {{{description}}}
    

    来自 fine manual 以下为:

    变量
    […]
    默认情况下,所有变量都是HTML转义的。如果您想返回无标题的HTML,请使用三重胡子: {{{name}}} 。

    所以 {{description}} 将由Mustache进行HTML编码,但是 {{{descriptipn}}} 将使用 描述 照原样。