How can I automatically render partials using markdown in Rails 3?

Have found way not to use haml in such situation.

in views/layouts/_markdown.html.erb

<%= m yield %>

in app/helpers/application_helper.rb

def m(string)
   RDiscount.new(string).to_html.html_safe
end  

in Gemfile

gem 'rdiscount'

So, in view you can call it like:

<%= render :partial => "contract.markdown", :layout => 'layouts/markdown.html.erb' %>

And contract.markdown will be formatted as markdown


Not a pure markdown solution but you can use HAML filters to render markdown, as well as other markup languages.

For example, in app/views/_my_partial.html.haml:

:markdown
  My awesome view
  ===============

  Look, I can **use** #{language}!

I just released a markdown-rails gem, which handles .html.md views.

You cannot chain it with Erb though -- it's only for static views and partials. To embed Ruby code, you'd have to use tjwallace's solution with :markdown.


Turns out, the Right Way (tm) to do this is using ActionView::Template.register_template_handler:

lib/markdown_handler.rb:

require 'rdiscount'

module MarkdownHandler
  def self.erb
    @erb ||= ActionView::Template.registered_template_handler(:erb)
  end

  def self.call(template)
    compiled_source = erb.call(template)
    "RDiscount.new(begin;#{compiled_source};end).to_html"
  end
end

ActionView::Template.register_template_handler :md, MarkdownHandler

If you require 'markdown_handler' in your config/application.rb (or an initializer), then any view or partial can be rendered as Markdown with ERb interpolation using the extension .html.md:

app/views/home/index.html.md:

My awesome view
===============

Look, I can **use** <%= @language %>!

app/controllers/home_controller.rb:

class HomeController < ApplicationController
  def index
    @language = "Markdown"
  end
end