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 << ... |
Get Crafting Rails 4 Applications, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.