Database Reference
In-Depth Information
Listing 3.3
HTML with embedded Ruby for rendering the Tweets
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style>
body {
background-color: #DBD4C2;
width: 1000px;
margin: 50px auto;
}
h2 {
margin-top: 2em;
}
</style>
</head>
<body>
<h1>Tweet Archive</h1>
<% TAGS.each do |tag| %>
<a href="/?tag=<%= tag %>"><%= tag %></a>
<% end %>
<% @tweets.each do |tweet| %>
<h2><%= tweet['text'] %></h2>
<p>
<a href="http://twitter.com/<%= tweet['from_user'] %>">
<%= tweet['from_user'] %>
</a>
on <%= tweet['created_at'] %>
</p>
<img src="<%= tweet['profile_image_url'] %>" width="48" />
<% end %>
</body>
</html>
Most of the code is just HTML with some ERB mixed in. 6 The important parts come
near the end, with the two iterators. The first of these cycles through the list of tags to
display links for restricting the result set to a given tag. The second iterator, beginning
with the @tweets.each code, cycles through each tweet to display the tweet's text, cre-
ation date, and user profile image. You can see results by running the application:
$ ruby viewer.rb
6
ERB stands for embedded Ruby . The Sinatra app runs the tweets.erb file through an ERB processor and eval-
uates any Ruby code between <% and %> in the context of the application.
Search WWH ::




Custom Search