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.


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

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

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

Then we need to create the controller.

class HandlersController < ApplicationController
  def merb

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)

    def self.call(template)
      compiled_source = erb_handler.call(template)

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.


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