2012-08-02 109 views
1

我正在嘗試用Haml + Mustache創建一個博客,但是'description'字段有一個CKEditor,因此該字段總是包含html標籤,但是鬍鬚不會呈現爲html,甚至不會顯示'description .html_safe」。Haml +小鬍子

我的助手:

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 

我的鬍子初始化:

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

我猜你正在做這樣的事情在你的鬍子:

{{description}} 

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

{{{description}}} 

fine manual

變量
[...]
所有變量都HTML默認情況下逃脫。如果您要返回未轉義的HTML,請使用三重小鬍子: {{{name}}}

所以{{description}}將鬍鬚被HTML編碼,但{{{descriptipn}}}將使用description原樣。

+0

完美,謝謝:D – 2012-08-02 18:56:09