
May 27th, 2007, 09:13 AM
|
|
Registered User
|
|
Join Date: Oct 2006
Posts: 19
Time spent in forums: 3 h 49 m 40 sec
Reputation Power: 0
|
|
listing
PHP Code:
require 'erb'
def serve_html(file)
send_this = "Hello world 2"
data = File.read(file)
template = ERB.new(data, 0, '%<>')
send_this = "Hello, world 3!"
puts send_this
puts("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
puts(template.result)
end
send_this="Hello world 1!"
serve_html('test.rhtml')
execution
Quote: Hello, world 3!
HTTP/1.0 200 OK
Content-type: text/html
<body>Hello world 1!</body> |
Comment out the send_this="Hello world 1" at the end of the listing and the error will appear. It appears that when the puts is xecuted template.result is out of scope.
It has something to do with bindings - I am new to ruby - don't ask me why.
This is what I used to get your code to work. It involves something known as bindings and is in the ERb examples
PHP Code:
require 'erb'
def serve_html(file)
b = binding
send_this = "Hello world"
data = File.read(file)
template = ERB.new(data, 0, '%<>')
puts("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
puts(template.result(b))
end
serve_html('test.rhtml')
Note the bracketed (b) in the template.result call.
|