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.
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
相关文章
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
I wrote about adding this to the Standard Library's ERB here http://hostiledeveloper.com/2015/05/28/working-with-templates-in-ruby-erb.html
It's surprisingly a pain in the butt.
You can use Tilt but if you don't want to add an extra dependency, here's a better example on how you can yield in erb:
I found the answer here.
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.
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).Check out Tilt (http://github.com/rtomayko/tilt/). It's the gem that handles templating in Sinatra and it provides ERB yields along with many other nice features.