Skip to content

Commit

Permalink
add content_for
Browse files Browse the repository at this point in the history
  • Loading branch information
tcrayford committed Apr 24, 2012
1 parent 0961501 commit 01c0e2e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
24 changes: 23 additions & 1 deletion lib/raptor/templates.rb
Expand Up @@ -34,7 +34,29 @@ def ==(other)
end

def render(inner, presenter)
Tilt.new(@path).render { inner.render(presenter) }
context = ViewContext.new(presenter)
rendered = inner.render(context)
Tilt.new(@path).render(context) { rendered }
end
end

class ViewContext < BasicObject
def initialize(presenter)
@presenter = presenter
@areas = {}
end

def content_for(name, &block)
if block
@areas[name] ||= []
@areas[name] << block.call
else
@areas[name].join
end
end

def method_missing(name, *args, &block)
@presenter.send(name, *args, &block)
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/layout_with_content_for.html.erb
@@ -0,0 +1 @@
<%= content_for :head %>
3 changes: 3 additions & 0 deletions spec/fixtures/provides_content_for.html.erb
@@ -0,0 +1,3 @@
<% content_for :head do %>
<script></script>
<% end %>
28 changes: 27 additions & 1 deletion spec/template_spec.rb
Expand Up @@ -25,13 +25,39 @@
end

describe Raptor::Layout do
let(:presenter) { stub }
it "renders a yielded template" do
inner = stub(:render => 'inner')
presenter = stub
rendered = Raptor::Layout.new('spec/fixtures/layout.html.erb').
render(inner, presenter)
rendered.strip.should == "<div>inner</div>"
end

it "integrates content_for" do
inner = Raptor::Template.from_path("../spec/fixtures/provides_content_for.html.erb")
layout = Raptor::Layout.new('spec/fixtures/layout_with_content_for.html.erb')
rendered = layout.render(inner, presenter)
rendered.strip.should include("<script></script")
end
end

describe Raptor::ViewContext do
it "delegates to the presenter" do
Raptor::ViewContext.new(stub(:cheese => 'walrus')).cheese.should == 'walrus'
end

it "stores content_for" do
context = Raptor::ViewContext.new(stub)
context.content_for(:head) { 'what' }
context.content_for(:head).should == 'what'
end

it "appends multiple content_for blocks" do
context = Raptor::ViewContext.new(stub)
context.content_for(:head) { 'what' }
context.content_for(:head) { 'what' }
context.content_for(:head).should == 'whatwhat'
end
end

describe Raptor::FindsLayouts do
Expand Down

0 comments on commit 01c0e2e

Please sign in to comment.