There are three ways to embed Ruby inside an HTML document:
<% ruby code %>
<%= ruby expression %>
<%# ruby code %>
If you are familiar with ASP, JSP, or PHP, this should be very familiar to you (except for maybe that last one). The following examples will be run through the eruby program directly. Here's an example of using the first form:
This is embedded <% bleh = "Ruby"; print "#{bleh}!" %>
This statement would produce: This is embedded Ruby!Here's an example of using the second form:
<%bleh = "Ruby"%>This is embedded <%=bleh%>!
The output is the same as before. The last form of embedding Ruby is the same as the first. Pretty simple, huh? eRuby really isn't complicated, so long as you already know Ruby ;] OK, so you can run this stuff through the eruby program, but how do you get a webserver to run this? Simple. I'm going to show you how to use eruby with Apache.The first thing you need to do is copy the eruby program itself into your cgi-bin directory. Then you need to edit your httpd.conf file.
Add these two lines:Quote: AddType application/x-httpd-eruby .rhtmlAction application/x-httpd-eruby /cgi-bin/eruby Now, whenever you want to use an HTML file with embedded Ruby, just rename the file with a .rhtml extension and you're ready to go. You can also add or replace your DirectoryIndex directive so that index.rhtml is searched for.
For example:Quote: DirectoryIndex index.html index.shtml index.rhtml Pretty easy. And, if you're using Apache and want faster access, you can always download and install mod_ruby.Well, that's it for now. For more information about eRuby, check out the Ruby documentation (that's where I got this info).
This article was originally written by vektor |