Search code examples
rubyerbpartials

How to implement erb partials in a non rails app?


I want to do something like this:

require 'erb'
@var = 'test'
template = ERB.new File.new("template.erb").read
rendered = template.result(binding())

But how can I use partials in template.erb?


Solution

  • Perhaps brute-force it?

    header_partial = ERB.new(File.new("header_partial.erb").read).result(binding)
    footer_partial = ERB.new(File.new("footer_partial.erb").read).result(binding)
    
    template = ERB.new <<-EOF
      <%= header_partial %>
      Body content...
      <%= footer_partial %>
    EOF
    puts template.result(binding)