Created
August 24, 2010 20:13
-
-
Save ggtesta/548225 to your computer and use it in GitHub Desktop.
A simple example of Effigy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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