views:

222

answers:

2

Hi

How can I use yield for template inheritance in erb? I want to use erb in a plain ruby CGI script and want to use a base template and subtemplate like it Rails with the application template does.

A: 

I don't think you can - Rails provides that infrastructure as part of actionpack.

What you may be able to do is take actionpack and add it into your script.

Alternatively you could roll a lightweight templating system yourself.

Alternatively alternatively use Rails or Merb or Sinatra.

Toby Hede
+1  A: 
def a
  ERB.new('<%= yield %>').result(binding)
end
a{123}
#=> "123"

It's important that the call to Kernel#binding be inside a method, that way the context includes the block (ERB#result won't take a block).

holizz