Webrick, erb, and .rhtml

B

Belorion

I am trying to get a basic Webrick server running to serve up .rhtml
files on a WinXP machine. I saw that you need to install erb to get
this to work (BTW, the erb link on the Webrick site is broken), so I
installed erb 2.0.4.

I startup a Webrick server using:

#!/usr/local/bin/ruby
require 'webrick'
include WEBrick

s = HTTPServer.new( :port=> 2000 )

## mount subdirectories
s.mount("/~test",
HTTPServlet::FileHandler, "c:\\documents and
settings\\user\\desktop\\test",
true) #<= allow to show directory index.

trap("INT"){ s.shutdown }
s.start


Serving up html works fine. However, serving up .rhtml does not work.
If I visit my test.rhtml, whose contents are:

<html><head></head><body>
testing, 1234
<%
10.times{ |i| puts i }
%>
</body></html>

All I get as output on my browser is "testing, 1234", whose source looks like
<html><head></head><body>
testing, 1234
</body></html>

At the command prompt, if I ruby "erb test.rhtml" I get:

0
1
2
3
4
5
6
7
8
9
<html>
<head>
</head>
<body>
testing, 1234


</body>
</html>

Which is obviously incorrect. What am I doing wrong here? I am using
ruby 1.8.2 (2004-07-29), and erb 2.0.4.
 
D

David A. Black

Hi --

Serving up html works fine. However, serving up .rhtml does not work.
If I visit my test.rhtml, whose contents are:

<html><head></head><body>
testing, 1234
<%
10.times{ |i| puts i }
%>
</body></html>

All I get as output on my browser is "testing, 1234", whose source looks like
<html><head></head><body>
testing, 1234
</body></html>

At the command prompt, if I ruby "erb test.rhtml" I get:

0
1
2
3
4
5
6
7
8
9
<html>
<head>
</head>
<body>
testing, 1234


</body>
</html>

Which is obviously incorrect. What am I doing wrong here? I am using
ruby 1.8.2 (2004-07-29), and erb 2.0.4.

You need <%= %> rather than <% %>. But keep in mind that what erb
will show you is the result of evaluating the expression. The result
of evaluating 10.times {...} is 10 -- and the result of evaluating
puts is nil. So if you want to display all the numbers, you'd want
something like:

<%= (1..10).to_a.join(", ") %>


David
 
B

Belorion

Thank you for the quick response!

Does there exist Webrick support for handling .rhtml files the way
eruby/mod_ruby does?

Matt
 
B

Belorion

You need said:
will show you is the result of evaluating the expression. The result
of evaluating 10.times {...} is 10 -- and the result of evaluating
puts is nil. So if you want to display all the numbers, you'd want
something like:

<%= (1..10).to_a.join(", ") %>

David

Further research shows me that on the Erb website
(http://www2a.biglobe.ne.jp/~seki/ruby/erb.html) that

Hello <% print "world" %>

Should give me:

Hello world

But this is not the case. Rather, I get worldHello. ??? Does anyone
else have this behaviour with Erb?
 
B

Bill Atkins

Try mounting HTTPServlet::ERBHandler instead of
HTTPServlet::FileHandler. It should run ERB on anything in the
directory it's mounted on.

Bill
 
B

Bill Atkins

print or puts will send their output to STDOUT - anything sent there
will go to the shell that you used to start webrick. ERb is simply
transforming a string, so you have to use <%= to get portions of that
string replaced with something else.

Bill
 
N

Nicholas Van Weerdenburg

You misread the page. It says exactly what you got. Notice it is
showing eruby and erb examples. The erb example gives what you got.

<% print "hello" %> goes to stdout.

<%= "hello" %> is a short-cut for something like <% response.print
"hello" %> which may or may not be/include stdout.

So depending on how stdout is handled during building the template, <%
print "hello" %> may or may not work. In the examples on the erb page,
it shows it working for eruby but not for erb.

Nick
 
B

Belorion

You misread the page. It says exactly what you got. Notice it is
showing eruby and erb examples. The erb example gives what you got.

Now I feel silly :p. Thanks.
<% print "hello" %> goes to stdout.

<%= "hello" %> is a short-cut for something like <% response.print
"hello" %> which may or may not be/include stdout.

So depending on how stdout is handled during building the template, <%
print "hello" %> may or may not work. In the examples on the erb page,
it shows it working for eruby but not for erb.

Nick

That's tough luck for me then. I have a significantly large rhtml
project that uses lots of puts and print because that works fine in
Apache/eRuby/mod_ruby. Guess I have a lot of busy work ahead of me,
changing all my puts/prints to <%= "" %> if I want to support Webrick
serving. Thanks for the help!

One last question... using ERBHanlde instead of FileHanlder, this
works fine (brining up index.html):

localhost:2000/~test/

However, if I use:

locahost:2000/~test/index.html

I get a permission denied error. Why? They are both accessing the same files...
 
G

GOTOU Yuuzou

In message said:
That's tough luck for me then. I have a significantly large rhtml
project that uses lots of puts and print because that works fine in
Apache/eRuby/mod_ruby. Guess I have a lot of busy work ahead of me,
changing all my puts/prints to <%= "" %> if I want to support Webrick
serving. Thanks for the help!

You can make a custom handler class using eruby command.

--
require "webrick"

class ERubyHandler < WEBrick::HTTPServlet::ERBHandler
ERuby = "/usr/local/bin/eruby"

def initialize(server, name)
super
@script_filename = name
end

def do_GET(req, res)
IO.popen("#{ERuby} #{@script_filename}"){|io|
res.body = io.read
}
res["content-type"] = "text/html"
end
end

# register new handler class
WEBrick::HTTPServlet::FileHandler.add_handler("rhtml", ERubyHandler)

s = WEBrick::HTTPServer.new:)Port=> 2000)
s.mount("/~test", WEBrick::HTTPServlet::FileHandler, Dir.pwd)
trap("INT"){ s.shutdown }
s.start
--
One last question... using ERBHanlde instead of FileHanlder, this
works fine (brining up index.html):

localhost:2000/~test/

However, if I use:

locahost:2000/~test/index.html

I get a permission denied error. Why? They are both accessing the same files...

What arguments did you pass to mount method?

ERBHandler cannot process directories. It is designed to be
used by FileHandler. However, it can be mounted on the
server with a eruby script filename like:

s.mount("/~test", ERBHandler, "/somewhere/index.html")
 
M

Martin DeMello

Belorion said:
That's tough luck for me then. I have a significantly large rhtml
project that uses lots of puts and print because that works fine in
Apache/eRuby/mod_ruby. Guess I have a lot of busy work ahead of me,
changing all my puts/prints to <%= "" %> if I want to support Webrick
serving. Thanks for the help!

Would simply overriding 'puts' and 'print' to return what they print
work? That way you only need to change <% to <%= .

martin
 
C

Charles Comstock

Martin said:
Would simply overriding 'puts' and 'print' to return what they print
work? That way you only need to change <% to <%= .

martin

Why is it that erb doesn't override kernel puts and print so they go to
ERBOUT instead of STDOUT. That way you wouldn't get weird orderings
when you use them and it would make more sense. If you actually want to
print output to stdout or anything like that then you explicitly call
STDOUT.print or .puts. I think alot of people assume that print and
puts output in the same interlaced order as the <% and <%= blocks, but
they really don't. Does anyone have a good reason why this should not
be the case?
Charles Comstock
 
N

Nicholas Van Weerdenburg

Is there an ERBOUT? How about doing:

$stdout_default=$stdout # or STDOUT
$stdout=ERBOUT

This works for $stdout=$stderr, but I haven't tried it with erb.

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top