From: Massimiliano Mirra Date: 2002-03-22T10:59:20+09:00 Subject: Re: erb: how to include(import,load) another erb file? On Fri, Mar 22, 2002 at 08:10:41AM +0900, m_seki@mva.biglobe.ne.jp wrote: > <%= import('footer.erb')%> > > In real applications (such as CGI), I always write the method which > build sub part in Ruby script. Can one pass variables to imported files this way? The following outputs 10, I'm trying to have it output 4 by setting the variable just before importing. I can do this with a global variable $a, is there a better way? --- nav.erb <%= a ||= 10 %> --- test.rb require 'erb/erbl' class Page def initialize(template) @erb = ERbLight.new(template) end def import(path) File.open(path) do |f| ERbLight.new(f.read).result end end def to_html @erb.result(binding) end end template = ' <%= a=4; import("nav.erb") %> ' print Page.new(template).to_html --- output 10 --- I tried passing a binding to import: def import(path, abinding) File.open(path) do |f| ERbLight.new(f.read).result(abinding) end end [...] template = ' <%= a=4; import("nav.rb", binding) %> ' But this has the strange effect of deleting everything that comes before <%= a=4; import ... %>.