4

It appears there's not much documentation on Rails template handlers. There's the included handlers, like RJS, ERB, and Builder, which offer some assistance.

I'm trying to implement my own, and I've succeeded, albeit with a bit of weird code, or possibly there's something I don't quite understand.

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

So what's weird is that I have to call inspect, otherwise Rails will try to eval the string as Ruby code.

I was under the impression that that's what include ActionView::...::Compilable did (which I'm not including in my code).

Now, if I make my template "compilable" (by using the include... statement), it still looks for the call method instead of the compile method.

So could anyone explain to me a bit more about how this works?

Thanks!

Ivan
  • 97,549
  • 17
  • 50
  • 58

2 Answers2

4

I've just been going through this problem myself. Basically rails expects the renderer's .call method to return ruby code that will render your template. It then dynamically generates a method which runs this code, and injects it into a module.

The module has all of the url/application helpers included, which means they're in scope for the template.

So, in answer to your question the solution is for .call to return some ruby code that outputs your rendered template as a string, or for it to render ruby code that invokes your template engine.

Matt
  • 883
  • 10
  • 14
0

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

Papipo
  • 2,799
  • 2
  • 23
  • 28