Virtua Creative
Virtua Creative

Reputation: 2113

How to output data from Yaml variables (written in markdown) into an HTML Haml file?

In a Yaml data file aaa.yml I have:

yyy: |
     [foo](http://example.com) bar.

I want to pull this data from a Haml file (zzz.html.haml).

1. If I do zzz.html.haml:

    %p
      = data.aaa.yyy

2. If I do:

    %p
      :markdown
        = data.aaa.yyy

How can I make it render:

<p><a href="http://example.com">foo</a> bar</p>

?

Upvotes: 1

Views: 1176

Answers (2)

Virtua Creative
Virtua Creative

Reputation: 2113

Yay! Turns out there's this helper, which was already in my config.rb but didn't work by itself:

def markdown(text)
  Tilt['markdown'].new { text }.render
end

When I installed this helper (from @matt's answer) with the first one:

def markdown s
  Kramdown::Document.new(s).to_html
end

Then = markdown(foo.bar) worked, which made my day!! :D

Upvotes: 0

matt
matt

Reputation: 79723

You have a string that contains markdown, and you want to render that markdown and include the result in your output.

One way would be to use the Haml :markdown filter. You can’t use normal Haml syntax inside a filter (like you’ve tried), but you can use interpolation inside #{...}, so you could do

:markdown
  #{data.aaa.yyy}

A better way, if the string is a standalone chunk, might be to create a helper method that renders markdown, and call that.

Wherever you have your helpers add something like:

def markdown s
  # I'm using kramdown here, but you can use whatever processor you prefer.
  Kramdown::Document.new(s).to_html
end

Then in your Haml:

= markdown(data.aaa.yyy)

Upvotes: 1

Related Questions