ERB
From DreamHost
eRuby is an implementation that allows embedding Ruby inside HTML or other document, similar to PHP. It comes in several implementations: eruby is in C and faster; ERB is cross-platform and ships with Ruby. Both can be used on Dreamhost.
[edit] Setup
Create this file as e.g. /home/me/example.com/cgi-bin/erb.cgi:
#!/usr/bin/env ruby require "cgi" require "erb" # This cgi object is available in your RHTML files cgi = CGI.new # Optionally, enable using gems you've installed per http://forum.dreamhosters.com/programming/43221-gem-install-broken.htm#Post43228 ENV['GEM_PATH'] = "#{ENV['GEM_PATH']}:/home/YOUR_NAME/.gems" # Optionally, require stuff here so you don't need to do this in each RHTML file require "rubygems" begin path = nil if (ENV['PATH_TRANSLATED']) path = ENV['PATH_TRANSLATED'] else file_path = ENV['REDIRECT_URL'].include?(File.basename(__FILE__)) ? ENV['SCRIPT_URL'] : ENV['REDIRECT_URL'] path = File.expand_path(ENV['DOCUMENT_ROOT'] + '/' + file_path) raise "Attempt to access invalid path: #{path}" unless path.index(ENV['DOCUMENT_ROOT']) == 0 end # So that working directory is not here but where the .rhtml is: Dir.chdir(File.dirname(path)) erb = File.open(path) { |f| ERB.new(f.read) } print cgi.header + erb.result(binding) rescue Exception => e print "Content-Type: text/html\n\n" print "<h1>ERB error</h1><p>#{e}</p>" end
The file must be in a web-accessible directory.
Make it executable:
chmod +x erb.cgi
Create or edit /home/me/example.com/.htaccess, adding this:
# If you want Apache to use index.rhtml in addition to index.php etc. DirectoryIndex index.rhtml index.php index.html index.htm AddHandler rubypage .rhtml Action rubypage /cgi-bin/erb.cgi
[edit] Test
Test it with a page like this, for example as /home/me/example.com/test.rhtml. The extension must be rhtml.
<html> <head> <title><%= title = "Testing!" %></title> </head> <body> <h1><%= title %></h1> <p>GEM_PATH: <%= ENV['GEM_PATH'] %></p> <p>Params: <%= cgi.params.inspect %></p> </body> </html>

