Implementing Rails 3 Template Handlers

There is not much documentation in the documentation for Rails templates. It includes handlers such as RJS, ERB, and Builder that offer some help.

I am trying to implement my own, and I succeeded, albeit with a bit of strange code, or perhaps with something that I do not quite understand.

class MyHandler < ActionView::Template::Handler def call(template) template.source.inspect end end 

So it is strange that I have to call inspect , otherwise Rails will try to evaluate the string as Ruby code.

I got the impression that this is what include ActionView::...::Compilable (which I do not include in my code).

Now, if I make my template "compiled" (using the include... statement include... ), it is still looking for the call method instead of the compile method.

So can anyone explain me a little more about how this works?

Thanks!

+4
source share
2 answers

Check out tilt and temple , I learned a lot about template engines reading their code.

0
source

I only experienced this problem myself. Basically, rails expect the renderer .call method to return a ruby ​​code that your template will display. It then dynamically generates a method that runs this code and injects it into the module.

All url / application helpers are included in the module, which means that they are available for the template.

So, in answer to your question, the solution for .call returns some ruby ​​code that displays your rendered template as a string, or in order to display the ruby ​​code that calls your template engine.

+4
source

All Articles