我正在尝试使用 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)