Skip to content

Instantly share code, notes, and snippets.

@joelbyler
Created October 24, 2012 01:31
Show Gist options
  • Save joelbyler/3943170 to your computer and use it in GitHub Desktop.
Save joelbyler/3943170 to your computer and use it in GitHub Desktop.
require 'erb'
def get_items()
['bread', 'milk', 'eggs', 'spam']
end
def get_template()
%{
<DOCTYPE html "-//W3C//DTD 1.0 //EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Shopping List for <%= @date.strftime('%A, %d %B %Y') %></title>
</head>
<body>
<h1>Shopping List for <%= @date.strftime('%A, %d %B %Y') %></h1>
<p>You need to buy:</p>
<ul>
<% for @item in @items %>
<li><%= h(@item) %></li>
<% end %>
</ul>
</body>
</html>
}
end
class ShoppingList
include ERB::Util
attr_accessor :items, :template, :date
def initialize(items, template, date=Time.now)
@date = date
@items = items
@template = template
end
def render()
ERB.new(@template).result(binding)
end
def save(file)
File.open(file, "w+") do |f|
f.write(render)
end
end
end
list = ShoppingList.new(get_items, get_template)
list.save(File.join(ENV['HOME'], 'list.html'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment