Matheus Caceres
Matheus Caceres

Reputation: 89

Haml + Mustache

I'm trying to create a blog with Haml + Mustache, but the field 'description' has a CKEditor, thus the field always contains html tags, but the Mustache doesn't render as html even putting 'description.html_safe'.

My Helper:

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

My Mustache initializer:

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)

Upvotes: 1

Views: 1118

Answers (1)

mu is too short
mu is too short

Reputation: 434665

I'm guessing that you're doing something like this in your Mustache:

{{description}}

If description contains HTML, you need to say:

{{{description}}}

From the fine manual:

Variables
[...]
All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}.

So {{description}} will be HTML encoded by Mustache but {{{descriptipn}}} will use description as-is.

Upvotes: 4

Related Questions