O'Reilly logo

Crafting Rails 4 Applications, 2nd Edition by José Valim

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Playing with the Template-Handler API

For an object to be compliant with the handler API, it needs to respond to the call method. This method receives as an argument an instance of ActionView::Template, which we introduced in Writing the Code, and should return a string containing valid Ruby code. The Ruby code the handler returns is then compiled into a method, so rendering a template is as simple as invoking this compiled method.

Before diving into our Markdown + ERB handler, let’s create a few template handlers to get acquainted with the API.

Ruby Template Handler

Our first template handler simply allows arbitrary Ruby code as a template. This means the following template is valid:

 
body = ​""
 
body << ​"This is my first "
 
body << ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required