Skip to content

Instantly share code, notes, and snippets.

@ggtesta
Created August 24, 2010 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggtesta/548225 to your computer and use it in GitHub Desktop.
Save ggtesta/548225 to your computer and use it in GitHub Desktop.
A simple example of Effigy
require "rubygems"
require "effigy"
template = %{
<html>
<head>
<title></title>
</head>
<body>
<h1></h1>
<p class="body"></p>
<div class="comment">
<h2></h2>
<p></p>
<a>View more</a>
</div>
<p id="no-comments">There arent any comments for this post.</p>
</body>
</html>
}
class Post
attr_accessor :title, :body, :comments
end
class Comment
attr_accessor :title, :summary
end
class PostView < Effigy::View
attr_reader :post
def initialize(post)
@post = post
end
def transform
text('h1', post.title)
text('title', "#{post.title} - Site title")
text('p.body', post.body)
# replace_each('.comment', post.comments) do |comment|
# text('h2', comment.title)
# text('p', comment.summary)
# attr('a', :href => url_for(comment))
# end
# remove('#no-comments') if post.comments.empty?
end
end
post = Post.new
post.title = "IPhone 4"
post.body = "Launched iPhone 4 on United States."
view = PostView.new(post)
document = view.render(template)
puts document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment