Markdown render in template side with Crinja – Crystal

2018 05 18 No Comments

Hello
In crystal ecr templates, we can use render markdown like this: <%= Markdown.to_html("# text") %>

But with crinja we cant do it as I see.

So I created template function.

require "markdown"

c = Crinja.new
c.loader = Crinja::Loader::FileSystemLoader.new("src/views/")

c.functions << Crinja.function({:str => ""}, :Markdown) do
  Markdown.to_html(arguments[:str].to_s)
end

To use it:

{{ Markdown("# title")  | safe}}

Enjoy.