This post originated from an RSS feed registered with Ruby Buzz
by Christopher Cyll.
Original Post: Making Ruby into PHP
Feed Title: Topher Cyll
Feed URL: http://feeds.feedburner.com/cyll
Feed Description: I'm not too worried about it. Ruby and programming languages.
Sometimes a web framework is just over kill. Maybe it's a one off
dynamic page, maybe you don't want the memory footprint of a whole
framework running for only a small component of your whole site.
mod_ruby and eruby can get you a lot of what PHP gives you if you're
into that and don't mind the setup. There's the added perk that you
won't have to write PHP.
But what if you want something similar, but quick and simple and
you're willing to use CGI? Here's a neat little trick.
First save this text into a file called "rubyhp.rb" in your cgi-bin
directory.
Leave this file unexecutable, so your web server won't serve it. Then
create your PHP style Ruby file like this.
#!/usr/bin/ruby
require 'rubyhp'
__END__
<html>
<body>
<% cgi.params.each do |key, value| %>
<%= key %>: <%= value %><br />
<% end %>
<% if cgi.params.empty? %>
Sorry, please enter some cgi parameters. How about "?foo=baz"?
<% end %>
</body>
</html>
Name this file "test.rb" and save it in your same cgi-bin
directory. Make it executable and you're done. You can write whatever
erb you want after the __END__ line, without needing to worry about
setting up anything fancy. And you have access to a parsed CGI object
through the variable 'cgi'.
Does CGI still have a place in modern web work? I'm don't know. But I
do still use it for quick dynamic pages on cyll.org like the Computer Science Bandname
Generator. This little trick makes life a little easier for me
when I do.