You do not give much details, but I think you have a servlet to serve the files that you will process with erb, and by default the web server serves any static file in the public directory.
require 'webrick' include WEBrick require 'erb' s = HTTPServer.new( :Port => 8080,:DocumentRoot => Dir::pwd + "/public" ) class MyServlet < HTTPServlet::AbstractServlet def do_GET(req, response) File.open('public/my.rhtml','r') do |f| @template = ERB.new(f.read) end response.body = @template.result(binding) response['Content-Type'] = "text/html" end end s.mount("/my", MyServlet) trap("INT"){ s.shutdown } s.start
This example is limited when you go to / my, the same file is always processed. Here you should build the file path based on the request path. Here I said an important word: "request", all you need is.
To get the HTTP header parameters, use req [header_name]. To get parameters in the query string, use req.query [param_name]. req - The HTTPRequest object passed to the servlet.
Once you have the required parameter, you should bind it to the template. In the example, we pass the binding object from ourselves (the binding is defined in Kernel, and it represents the context where the code is executed), so each local variable defined in the do_GET method will be available in the template. However, you can create your own binding, for example, pass a Proc object and pass it to the ERB processor when you call "result".
All together, your solution will look like this:
def do_GET(req, response) File.open('public/my.rhtml','r') do |f| @template = ERB.new(f.read) end foo = req.query["foo"] response.body = @template.result(binding) response['Content-Type'] = "text/html" end
source share