Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of git://github.com/darthapo/webby into darthap…

…o/master
  • Loading branch information...
commit 4e7d42f83c6c090f327b40c9b9888427611fedfb 2 parents c5515ce + e2abf75
@TwP TwP authored
View
29 lib/webby/helpers/capture_helper.rb
@@ -0,0 +1,29 @@
+module CaptureHelper
+
+ # Based on code from Rails
+
+ def content_for(name, content = nil, &block)
+ existing_content_for = instance_variable_get("@content_for_#{name}").to_s
+ new_content_for = existing_content_for + (block_given? ? capture(&block) : content)
+ instance_variable_set("@content_for_#{name}", new_content_for)
+ end
+
+ def capture_erb(*args, &block)
+ buffer = eval('_erbout', block.binding)
+ capture_erb_with_buffer(buffer, *args, &block)
+ end
+ alias_method :capture, :capture_erb
+ alias_method :capture_block, :capture_erb
+
+ def capture_erb_with_buffer(buffer, *args, &block)
+ pos = buffer.length
+ block.call(*args)
+ # extract the block
+ data = buffer[pos..-1]
+ # replace it in the original with empty string
+ buffer[pos..-1] = ''
+ data
+ end
+end # module CaptureHelper
+
+Webby::Helpers.register(CaptureHelper)
View
42 spec/webby/helpers/capture_helper_spec.rb
@@ -0,0 +1,42 @@
+
+require ::File.join(::File.dirname(__FILE__), %w[.. .. spec_helper])
+
+# ---------------------------------------------------------------------------
+describe CaptureHelper do
+ CFN = ::File.expand_path(::File.join(::File.dirname(__FILE__), '..', '..', 'capture_for_yaml.txt'))
+ CLINES = [
+ "--- ",
+ "filter: ",
+ " - erb ",
+ "--- ",
+ "Hello world!",
+ "<% content_for :sidebar do %>",
+ "I'm sidebar content.",
+ "<% end %>"
+ ]
+
+ before do
+ ::File.open(CFN,'w') {|fd| fd.write CLINES.join("\n") }
+ @page = Webby::Resources::Page.new(CFN)
+ @renderman = Webby::Renderer.new(@page)
+ @page_content = @page.render( @renderman )
+ end
+
+ after do
+ ::FileUtils.rm_f(CFN)
+ end
+
+
+ it 'should not "leak" any content to containing page' do
+ @page_content.should_not be_nil
+ @page_content.should eql("Hello world!\n")
+ end
+
+ it "should create an instance variable containing the nested content" do
+ @renderman.instance_variable_get("@content_for_sidebar").should_not be_nil
+ @renderman.instance_variable_get("@content_for_sidebar").should eql("\nI'm sidebar content.\n") # Note: Leading newline
+ end
+
+end # describe Webby::Resources::File
+
+# EOF
Please sign in to comment.
Something went wrong with that request. Please try again.