Skip to content

Commit

Permalink
Refactor render to class
Browse files Browse the repository at this point in the history
  • Loading branch information
steveklabnik committed Mar 16, 2012
1 parent ccbdf0a commit c74cecb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 2 additions & 0 deletions config.ru
Expand Up @@ -5,6 +5,8 @@ require 'webmachine/adapters/rack'

Dir["resources/*"].each {|f| require "./#{f}" }

require "./render"

Shoes = Webmachine::Application.new do |app|
app.routes do
add [], ShoesHomepage
Expand Down
13 changes: 13 additions & 0 deletions render.rb
@@ -0,0 +1,13 @@
require 'haml'

class Render
def initialize(view)
@view = view
end

def render(locals={})
Haml::Engine.new(File.read("views/layout.html.haml")).render do
Haml::Engine.new(File.read("views/#{@view}.html.haml")).render(Object.new, locals)
end
end
end
12 changes: 5 additions & 7 deletions resources/blog_resource.rb
Expand Up @@ -19,12 +19,10 @@ def resource_exists?
def to_html
return "INDEX" unless @post

Haml::Engine.new(File.read("views/layout.html.haml")).render do
Haml::Engine.new(File.read("views/post.html.haml")).render(Object.new,
:contents => @post.output,
:title => @post.metadata["title"],
:date => @post.metadata["date"]
)
end
Render.new(:post).render(
:contents => @post.output,
:title => @post.metadata["title"],
:date => @post.metadata["date"]
)
end
end
6 changes: 1 addition & 5 deletions resources/shoes_homepage.rb
@@ -1,9 +1,5 @@
require 'haml'

class ShoesHomepage < Webmachine::Resource
def to_html
Haml::Engine.new(File.read("views/layout.html.haml")).render do
Haml::Engine.new(File.read("views/index.html.haml")).render
end
Render.new(:index).render
end
end

0 comments on commit c74cecb

Please sign in to comment.