public
Rubygem
Description: A lightweight and flexible website management system.
Homepage: http://webby.rubyforge.org/
Clone URL: git://github.com/TwP/webby.git
Search Repo:
Updated how the content_for code works. It is slightly different -- a 
blending
of the Rails and Merb tehcniques. Instead of creating an instance variable 
for
each hunk of content, they are all stored in a @_content_for hash. Content 
is
stored and retrieved using the same method -- if you pass it arguments 
then you
are storing content; without arguments you are retrieving content.
TwP (author)
Sun Jun 01 15:02:18 -0700 2008
commit  9ef7e0acd86cec85e798e02c5905b7b95e79e818
tree    ca257fa0b132df65287aa5162982e24a277b6d4b
parent  201dd1e10cdbee618556d8aa7f591234d0eaa210
...
5
6
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
9
10
11
12
 
 
 
 
13
14
15
 
 
 
 
 
 
 
 
 
 
 
 
 
16
17
18
 
 
19
20
21
 
22
23
24
25
26
 
 
 
 
 
 
 
 
 
 
27
28
 
 
 
 
29
30
31
 
32
33
34
35
36
37
 
38
39
 
40
41
42
...
56
57
58
59
 
60
61
62
63
64
65
66
 
 
 
 
 
 
 
 
 
67
68
69
 
 
 
 
 
 
 
 
 
 
 
 
70
71
72
...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
 
 
 
32
33
34
35
36
 
 
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 
52
53
54
55
 
56
57
58
 
 
 
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
77
78
79
80
81
82
 
83
84
 
85
86
87
88
...
102
103
104
 
105
106
107
108
109
110
 
 
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
0
@@ -5,38 +5,84 @@ module Webby::Helpers
0
 #
0
 module CaptureHelper
0
 
0
+ # Called in pages and partials to store up content for later use. Takes a
0
+ # string and/or a block. First, the string is evaluated, and then the
0
+ # block is captured using the capture() helper provided by the template
0
+ # languages. The two are concatenated together.
0
+ #
0
+ # Content is retrieved by calling the method without a string or a block.
0
+ #
0
+ # ==== Parameters
0
+ # obj<Object>:: The key in the conetnt_for hash.
0
+ # string<String>:: Textual content. Defaults to nil.
0
+ # &block:: A block to be evaluated and concatenated to string.
0
+ #
0
+ # ==== Returns
0
+ # Any content associated with the key (or nil).
0
+ #
0
+ # ==== Example
0
+ # content_for(:foo, "Foo")
0
+ # content_for(:foo) #=> "Foo"
0
+ # content_for(:foo, "Bar")
0
+ # content_for(:foo) #=> "FooBar"
0
+ #
0
+ def content_for( obj, string = nil, &block )
0
+ return @_content_for[obj] unless string || block_given?
0
 
0
- def content_for( name, content = nil, &block )
0
- cur = instance_variable_get("@content_for_#{name}").to_s
0
- new = if block.nil? then content
0
- else capture_erb(&block) end
0
+ cur = @_content_for[obj].to_s
0
+ new = string.to_s + (block_given? ? capture_erb(&block) : "")
0
+ @_content_for[obj] = cur + new
0
+ end
0
 
0
- new = cur + new.to_s
0
- instance_variable_set("@content_for_#{name}", new)
0
+ # Returns true if there is content for the given key. Otherwise returns
0
+ # false.
0
+ #
0
+ # ==== Parameters
0
+ # obj<Object>:: The key in the conetnt_for hash.
0
+ #
0
+ # ==== Example
0
+ # content_for(:foo, "Foo")
0
+ # content_for?(:foo) #=> true
0
+ # content_for?(:bar) #=> false
0
+ #
0
+ def content_for?( obj )
0
+ @_content_for.key?(obj)
0
   end
0
 
0
- # Provides direct acccess to the buffer for this view context
0
+ # Deletes any content associated with the given object in the content_for
0
+ # hash.
0
   #
0
   # ==== Parameters
0
- # the_binding<Binding>:: The binding to pass to the buffer.
0
+ # obj<Object>:: The key in the conetnt_for hash.
0
   #
0
   # ==== Returns
0
- # DOC
0
- def _erb_buffer( the_binding )
0
- eval("_erbout", the_binding, __FILE__, __LINE__)
0
+ # Any content associated with the key (or nil).
0
+ #
0
+ # ==== Example
0
+ # content_for(:foo, "Foo")
0
+ # content_for?(:foo) #=> true
0
+ # delete_content_for(:foo)
0
+ # content_for?(:foo) #=> false
0
+ #
0
+ def delete_content_for( obj )
0
+ @_content_for.delete(obj)
0
   end
0
 
0
+ # This method is used to capture content from an ERB filter evaluation. It
0
+ # is useful to helpers that need to process chunks of data during ERB filter
0
+ # processing.
0
+ #
0
   # ==== Parameters
0
   # *args:: Arguments to pass to the block.
0
- # &block:: The template block to call.
0
+ # &block:: The ERB block to call.
0
   #
0
   # ==== Returns
0
   # String:: The output of the block.
0
   #
0
   # ==== Examples
0
- # Capture being used in a .html.erb page:
0
+ # Capture being used in an ERB page:
0
   #
0
- # <% @foo = capture do %>
0
+ # <% @foo = capture_erb do %>
0
   # <p>Some Foo content!</p>
0
   # <% end %>
0
   #
0
@@ -56,17 +102,36 @@ module CaptureHelper
0
       data = buffer[pos..-1]
0
 
0
       # replace it in the original with empty string
0
- buffer[pos..-1] = ''
0
+ buffer[pos..-1] = ""
0
 
0
       data
0
     end
0
   end
0
 
0
- # DOC
0
- def concat_erb(string, the_binding)
0
+ # This method is used to concatenate content into the ERB output buffer.
0
+ # It is usefule to helpers that need to insert transformed text back into
0
+ # the ERB output buffer.
0
+ #
0
+ # ==== Parameters
0
+ # string<String>:: The string to insert into the ERB output.
0
+ # the_binding<Binding>:: The binding to pass to the buffer.
0
+ #
0
+ def concat_erb( string, the_binding )
0
     _erb_buffer(the_binding) << string
0
   end
0
 
0
+ # Provides direct acccess to the ERB buffer in the conext of the binding.
0
+ #
0
+ # ==== Parameters
0
+ # the_binding<Binding>:: The binding to pass to the buffer.
0
+ #
0
+ # ==== Returns
0
+ # The current ERB output buffer.
0
+ #
0
+ def _erb_buffer( the_binding )
0
+ eval("_erbout", the_binding, __FILE__, __LINE__)
0
+ end
0
+
0
 end # module CaptureHelper
0
 
0
 register(CaptureHelper)
...
63
64
65
 
66
67
68
...
63
64
65
66
67
68
69
0
@@ -63,6 +63,7 @@ class Renderer
0
     @config = ::Webby.site
0
 
0
     @_bindings = []
0
+ @_content_for = {}
0
     @log = Logging::Logger[self]
0
   end
0
 
...
1
2
 
 
3
4
5
...
15
16
17
18
 
19
20
21
22
23
24
25
26
 
 
 
 
 
27
28
 
 
 
29
30
31
32
33
34
35
36
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
39
40
 
41
42
...
1
 
2
3
4
5
6
...
16
17
18
 
19
20
 
 
 
21
 
 
 
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
 
 
 
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
54
55
56
0
@@ -1,5 +1,6 @@
0
 
0
-require ::File.join(::File.dirname(__FILE__), %w[.. .. spec_helper])
0
+require ::File.expand_path(
0
+ ::File.join(::File.dirname(__FILE__), %w[.. .. spec_helper]))
0
 
0
 # ---------------------------------------------------------------------------
0
 describe Webby::Helpers::CaptureHelper do
0
@@ -15,28 +16,41 @@ describe Webby::Helpers::CaptureHelper do
0
     "<% end %>"
0
   ]
0
 
0
- before do
0
+ before :all do
0
     ::File.open(CFN,'w') {|fd| fd.write CLINES.join("\n") }
0
- @page = Webby::Resources::Page.new(CFN)
0
- @renderman = Webby::Renderer.new(@page)
0
- @page_content = @page.render( @renderman )
0
   end
0
-
0
- after do
0
- ::FileUtils.rm_f(CFN)
0
+
0
+ before :each do
0
+ @renderman = Webby::Renderer.new(
0
+ Webby::Resources::Page.new(CFN))
0
+ @page_content = @renderman.render_page
0
   end
0
 
0
+ after :all do
0
+ ::FileUtils.rm_f(CFN)
0
+ end
0
 
0
   it 'should not "leak" any content to containing page' do
0
     @page_content.should_not be_nil
0
     @page_content.should eql("Hello world!\n")
0
   end
0
-
0
- it "should create an instance variable containing the nested content" do
0
- @renderman.instance_variable_get("@content_for_sidebar").should_not be_nil
0
- @renderman.instance_variable_get("@content_for_sidebar").should eql("\nI'm sidebar content.\n") # Note: Leading newline
0
+
0
+ it "should return the stored content for the given key" do
0
+ @renderman.content_for(:sidebar).should_not be_nil
0
+ @renderman.content_for(:sidebar).should eql("\nI'm sidebar content.\n") # Note: Leading newline
0
+ end
0
+
0
+ it "should report if content is associated with a given key" do
0
+ @renderman.content_for?(:sidebar).should == true
0
+ @renderman.content_for?(:header).should == false
0
+ end
0
+
0
+ it "should clear content associated with a given key" do
0
+ @renderman.content_for?(:sidebar).should == true
0
+ @renderman.delete_content_for(:sidebar)
0
+ @renderman.content_for?(:sidebar).should == false
0
   end
0
 
0
-end # describe Webby::Resources::File
0
+end
0
 
0
 # EOF

Comments

    No one has commented yet.