Creating a Rails3 Custom Template Handler

While the technique for creating Rails 3 custom template handlers is not particularly well-documented, it was pretty easy to setup. Here’s the code that I used for Jsonify.

require 'action_view'

module ActionView
  module Template::Handlers
    class JsonifyBuilder < Template::Handler
      include Compilable

      self.default_format = Mime::JSON

      def compile(template)
        "json = ::Jsonify::Builder.new();" +
          template.source +
        ";json.compile!;"
      end
    end
  end
end

ActionView::Template.register_template_handler :jsonify, ActionView::Template::Handlers::JsonifyBuilder

Then in my gemspec I added …

s.add_dependency "actionpack", "~> 3.0.0"

My next task … implement unit tests for this code … any tips?

Posterous theme by Cory Watilo