It looks like ERBHandler is unfinished -- either that or I don't
understand how it would be used.
I've had a server that used ERB for templates, and I converted it to
WEBrick (much less code than I had originally), then wanted to switch
my ERB code over to ERBHandler.
But I couldn't because ERBHandler didn't do what I needed...
I am new to Ruby, but here is my ERBServlet, which is closer to what I
think an ERBHandler should be:
require 'webrick'
require 'stringio'
require 'rexml/document'
include WEBrick
class ERBServlet < HTTPServlet::AbstractServlet
def initialize( server, path )
super( server, path )
@mypath = path
end
def do_GET(req, resp)
#
# Here I am getting each value from URL of the type "a=b"
# and doing "eval" to create a global Ruby variable that can be
# used in the HTML page.
#
req.query.each {|qk,qv|
evalStr = "$" + qk + "=\"" + qv + "\""
eval( evalStr )
}
#
# Page keeps getting cached, and I don't want it cached, so
# I keep generating a different URL, then stripping it off here.
# There has got to be a better way!
#
path = req.path.split("_")[0]
pathToFile = @mypath + path
print "In my ERBServlet\n"
File.open( pathToFile ) { |fh|
erb = ERB.new( fh.read )
resp.body = erb.result( binding )
}
raise HTTPStatus::OK
end
end
def startWebServer( port )
config = {}
config.update( :DocumentRoot => Dir.pwd, :Port => port )
server = HTTPServer.new(config)
server.mount("/erb/", ERBServlet, ".")
yield server if block_given?
['INT', 'TERM'].each {|signal|
trap(signal) {server.shutdown}
}
server.start
end
print "To test, go to url: http://localhost:8080/erb/test.html\n\n"
startWebServer( 8080 )
If you take the above, put it in a directory, then create a
subdirectory called "erb" and put the following as "test.html" in that
"erb" directory, you'll see how I am using ERB for web pages.
Here is the "test.html":
<html>
<body>
<% testVariable = 3 %>
The test variable was set in ruby code to 3, its value here is: <%=
testVariable %><br>
This variable was set from URL, using "&name=value" at end
of the URL, so <a
href="http://localhost:8080/erb/test.html?bogus=7">this URL sets it to
7</a>, and <a href="http://localhost:8080/erb/test.html?bogus=8">this
URL sets it to 8</a><br><br>
The Variable "bogus" has value: <%= $bogus %>
</body>
</html>
Keep in mind I may be totally misunderstanding ERBHandler, but as for
my implementation, the end result is:
- any template in directory is processed by ERB (with ERBHandler it
looks like same template used in all cases)
- variables that are part of the URL get "eval"ed into global Ruby
variables (this is the only way I could get this to work, but I
suspect there are better ways...)
The reason I needed this was that I created a long running simulation
program, and I wanted HTML views of it... so if the simulation is a
global Ruby variable, the above ERBServlet in a server thread along
with the simulation in another thread -- that combination lets you
change the HTML views of the simulation while it is running.
My question from those more experienced here is -- am I missing
something regarding how to use ERBHandler?
and -- is there a better way to implement what I did above? I like
it, just because it works, but I don't know Ruby all that well.