From: Alan Chen Date: 2002-02-21T04:01:25+09:00 Subject: Re: including files in eruby On Thu, Feb 21, 2002 at 03:33:29AM +0900, Paul Brannan wrote: > On Thu, Feb 21, 2002 at 03:26:44AM +0900, Alan Chen wrote: > > I've just been require'ing the files as normal ruby code. I've been > > prototyping an ruby based web application and use requires to separate > > the ruby application logic from the eruby presentation code. > > I don't know if this is the "best" way, but I've been doing something > like this: > > myfile.cgi: > File.open('myfile.rhtml') do |input| > eval <<-END > MY_TEMPLATE = proc { |var1, var2, var3| > #{ERuby::Compiler.new.compile_string(input.read())} > } > END > end > > MY_TEMPLATE.call(1, 2, 3) > > myfile.rhtml: > > > <%= var1 %>
> <%= var2 %>
> <%= var3 %>
> > > > Paul Actually, I've been using mod_ruby too so I just have the rhtml file. :) Its not perfect separation of logic and presentation, but eruby lets me draw the line between the two wherever I'd like. Generally it goes something like this <% require 'opencds' # the name of my web app require 'cgi' # occasionally there is page-specific logic here cgi = CGI.new case_record = OpenCDS::Case.new(cgi.params['case_id']) %>

Case ID: <%=case_record.id%>

Somewhere in my load path I have ruby code as follows opencds.rb: module OpenCDS class Case def initialize(case_id) #validate case id, retreive stuff from database end end end -- Alan Chen Digikata LLC http://digikata.com