Ryan C. wrote in post #983261:
Here's what I've got:
...
puts <<HTML
<!DOCTYPE html>
<html>
<head>
<title>Oliver Twist</title>
<style type ="text/css">
body {
background-color:#335;
color:#fff;
font-family:Helvetica,Arial,Verana,sans-serif;
font-size:12px;}
h1 {color:#c00}
</style>
</head>
<body>
<% File.open("oliver_twist.txt").each { |line| puts line } %>
</body>
</html>
HTML
puts inside puts is bad.
Look, what you're doing is simply this:
puts "A very big string indeed"
The only difference is that you've written the string in the multiline
form, also known as a "here-doc":
puts <<HTML
A very big string indeed
HTML
So, move your other puts outside this.
puts <<HTML
My header goes here
HTML
File.open("oliver_twist.txt").each { |line| puts line }
puts <<HTML
My footer goes here
HTML
As others have pointed out, if you get the contents of the file as a
*value* then you can insert it directly into a string. This is called
string interpolation.
puts <<HTML
My header goes here
#{File.read("oliver_twist.txt")}
My footer goes here
HTML
It looks like you would rather be using erb syntax, which is in any case
a much easier way to assemble a page.
It is possible to do this within a standalone cgi. However, you *really*
should look at Sinatra. Honestly. I promise you'll be glad you did. For
one thing it has support for erb templates built in; for another, it
makes it easier to separate your app logic from your view generation.
Here is your CGI as a sinatra app, complete with a layout and inline
templates in a single file.
···
----------------------------------------------------------------
require "rubygems" # ruby 1.8.x only
require "sinatra"
get "/" do
@title = "Oliver Twist"
@content = File.read("oliver_twist.txt")
erb :book
end
__END__
@@ layout
<!DOCTYPE html>
<html>
<head>
<title><%= @title %></title>
<style type ="text/css">
body {
background-color:#335;
color:#fff;
font-family:Helvetica,Arial,Verana,sans-serif;
font-size:12px;}
h1 {color:#c00}
</style>
</head>
<body>
<%= yield %>
</body>
</html>
@@ book
<pre>
<%= @content %>
</pre>
----------------------------------------------------------------
Do "gem install sinatra", then just run this code from the command line
(ruby myapp.rb). It will start a webserver listening on port 4567. There
are other options for when you want to deploy the app for real.
HTH, Brian.
--
Posted via http://www.ruby-forum.com/.