Benito Serna Tips and tools for Ruby on Rails developers

Making a template handler with Markdown and ERb

June 17, 2011

What you need to have an object compliant with the handler API, is to respond to the call method which receives an instance of ActionView::Template and should return a string with valid Ruby code.

Then the Ruby code returned by the handler is compiled into a method, and rendering the template turns into an invocation to this compiled method.

Start

Lets do it with enginex:

enginex handlers

First the test

Now we can write a little test for the .merb handler.

test '.merb template handler' do
  visit '/handlers/merb'
  expected = '<p>MERB is <strong>cool and fast</strong>!</p>'
  assert_match expected, page.body.strip
end

The test needs the route handlers/merb, so lets define it.

Dummy::Application.routes.draw do
  get "/handlers/merb", :to => "handlers#merb"
end

Then we need to create the controller.

class HandlersController < ApplicationController
  def merb
  end
end

Now we create the template.

MERB is **<%= %w(cool fast).to_sentence %>**!

Now make the test green

First we need to RDiscount (a Ruby wrapper to the fast Markdown compiler library called Discount, written in C) to our gem file.

gem "rdiscount", "1.6.5"

To implement the template handler, we can create a module that responds to call.

An then we can use the ActionView::Template.registeredtemplatehandler method to retrieve the ERb handler.

This code will be added to our lib/handlers.rb

require "action_view/template" 

module Handlers
  module MERB
    def self.erb_handler
      @@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
    end

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

ActionView::Template.register_template_handler :merb, Handlers::MERB

Here the erb_handler compiles the template, and returns a string with valid Ruby code (like any other template handler). This string contains Markdown syntax and is then converted to HTML using RDiscount.

References

The code here is from the book Crafting Rails Applications (You should read this book) by Jose Valim

Related articles

Weekly tips and tools for Ruby on Rails developers

I send an email each week, trying to share knowledge and fixes to common problems and struggles for ruby on rails developers, like How to fetch the latest-N-of-each record or How to test that an specific mail was sent or a Capybara cheatsheet. You can see more examples on Most recent posts or All post by topic.