public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
<%= now takes a block in Erubis. Erubis buffer now an ivar (@_erb_buf) so 
eval is no longer required for capture. Helpers that take a block should 
now return a string.
wycats (author)
Sun Jun 22 18:37:13 -0700 2008
commit  7043503871759eadc947bc5138ebbc96b8e17d74
tree    28d5d1835e98962007a98e671ae73df979014a2a
parent  0e44eca7bd69e325b687d2174f054ece61910ad0
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -0,0 +1,14 @@
0
+6/22/2008:
0
+
0
+Erubis modified:
0
+* <%= %> now can take a block, so <%= helper do %>Hello<% end %> now works
0
+* Erubis buffer is now an ivar (@_erb_buf), which eliminates the need for
0
+ eval in capture helpers.
0
+
0
+CONSEQUENCE:
0
+Helpers that take a block should simply return a string, and should not
0
+use concat. Example:
0
+
0
+ def my_helper(&blk)
0
+ "My helper says #{capture(&blk)}."
0
+ end
0
\ No newline at end of file
...
4
5
6
7
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
10
11
12
 
13
14
 
15
16
 
17
18
19
...
22
23
24
25
26
27
28
29
30
 
31
32
33
 
 
34
35
 
 
 
 
36
37
38
39
40
41
42
43
 
 
44
...
4
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
...
35
36
37
 
 
 
 
38
 
39
40
 
 
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 
55
56
57
58
0
@@ -4,16 +4,29 @@ require "erubis"
0
 module Erubis
0
   module BlockAwareEnhancer
0
     def add_preamble(src)
0
- src << "@_buf = '';"
0
- end
0
+ src << "_old_buf, @_erb_buf = @_erb_buf, ''; "
0
+ end
0
+
0
+ def add_postamble(src)
0
+ src << "\n" unless src[-1] == ?\n
0
+ src << "_ret = @_erb_buf; @_erb_buf = _old_buf; _ret.to_s\n"
0
+ end
0
+
0
+ def add_text(src, text)
0
+ src << " @_erb_buf << '" << text << "'; "
0
+ end
0
+
0
+ def add_expr_escaped(src, code)
0
+ src << ' @_erb_buf << ' << escaped_expr(code) << ';'
0
+ end
0
     
0
     def add_expr_literal(src, code)
0
       unless code =~ /(do|\{)(\s*|[^|]*|)?\s*$/
0
- src << ' _buf << (' << code << ').to_s;'
0
+ src << ' @_erb_buf << (' << code << ').to_s;'
0
       else
0
- src << ' _buf << ' << code << "; "
0
+ src << ' @_erb_buf << ' << code << "; "
0
       end
0
- end
0
+ end
0
   end
0
   
0
   class BlockAwareEruby < Eruby
0
@@ -22,21 +35,22 @@ module Erubis
0
 end
0
 
0
 class Context
0
- def _buf
0
- @_buf ||= ""
0
- end
0
-
0
   def capture(&blk)
0
- _old_buf, @_buf = @_buf, ""
0
+ _old_buf, @_erb_buf = @_erb_buf, ""
0
     blk.call
0
- ret = @_buf
0
- @_buf = _old_buf
0
+ ret = @_erb_buf
0
+ @_erb_buf = _old_buf
0
     ret
0
   end
0
+
0
+ def hello
0
+ "Hello"
0
+ end
0
 
0
   def helper(&blk)
0
     "<tag>#{capture(&blk)}</tag>"
0
   end
0
 end
0
 
0
-p Erubis::BlockAwareEruby.new.process("Begin: <%= helper do %>Hello<% end %><% 3.times do %>X<% end %>", Context.new)
0
\ No newline at end of file
0
+puts Erubis::BlockAwareEruby.new("Begin: <%= helper do %>Hello<% end %><% 3.times do %>X<% end %><%= hello %>").src
0
+p Erubis::BlockAwareEruby.new.process("Begin: <%= helper do %>Hello<% end %><% 3.times do %>X<% end %><%= hello %>", Context.new)
0
\ No newline at end of file
...
156
157
158
159
 
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
 
 
 
 
 
210
211
212
213
214
 
215
216
217
...
222
223
224
225
 
 
 
 
 
226
227
228
229
230
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
233
234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
236
...
156
157
158
 
159
160
161
162
163
164
165
 
 
 
 
 
 
 
 
 
 
 
166
167
168
...
177
178
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
181
182
183
184
185
186
187
188
 
189
190
191
192
...
197
198
199
 
200
201
202
203
204
205
 
 
 
 
 
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
 
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
0
@@ -156,24 +156,13 @@ module Merb::Template
0
     # name<String>:: The name of the method that will be created.
0
     # mod<Module>:: The module that the compiled method will be placed into.
0
     def self.compile_template(io, name, mod)
0
- template = ::Erubis::Eruby.new(io.read)
0
+ template = ::Erubis::BlockAwareEruby.new(io.read)
0
       template.def_method(mod, name, File.expand_path(io.path))
0
       name
0
     end
0
 
0
     module Mixin
0
       
0
- # Provides direct acccess to the buffer for this view context
0
- #
0
- # ==== Parameters
0
- # the_binding<Binding>:: The binding to pass to the buffer.
0
- #
0
- # ==== Returns
0
- # DOC
0
- def _erb_buffer( the_binding )
0
- @_buffer = eval( "_buf", the_binding, __FILE__, __LINE__)
0
- end
0
-
0
       # ==== Parameters
0
       # *args:: Arguments to pass to the block.
0
       # &block:: The template block to call.
0
@@ -188,30 +177,16 @@ module Merb::Template
0
       # <p>Some Foo content!</p>
0
       # <% end %>
0
       def capture_erb(*args, &block)
0
- # get the buffer from the block's binding
0
- buffer = _erb_buffer( block.binding ) rescue nil
0
-
0
- # If there is no buffer, just call the block and get the contents
0
- if buffer.nil?
0
- block.call(*args)
0
- # If there is a buffer, execute the block, then extract its contents
0
- else
0
- pos = buffer.length
0
- block.call(*args)
0
-
0
- # extract the block
0
- data = buffer[pos..-1]
0
-
0
- # replace it in the original with empty string
0
- buffer[pos..-1] = ''
0
-
0
- data
0
- end
0
+ _old_buf, @_erb_buf = @_erb_buf, ""
0
+ block.call
0
+ ret = @_erb_buf
0
+ @_erb_buf = _old_buf
0
+ ret
0
       end
0
 
0
       # DOC
0
       def concat_erb(string, binding)
0
- _erb_buffer(binding) << string
0
+ @_erb_buf << string
0
       end
0
             
0
     end
0
@@ -222,14 +197,46 @@ module Merb::Template
0
 end
0
 
0
 module Erubis
0
- module RubyEvaluator
0
+ module BlockAwareEnhancer
0
+ def add_preamble(src)
0
+ src << "_old_buf, @_erb_buf = @_erb_buf, ''; "
0
+ src << "@_engine = 'erb'; "
0
+ end
0
 
0
- # DOC
0
- def def_method(object, method_name, filename=nil)
0
- m = object.is_a?(Module) ? :module_eval : :instance_eval
0
- setup = "@_engine = 'erb'"
0
- object.__send__(m, "def #{method_name}(locals={}); #{setup}; #{@src}; end", filename || @filename || '(erubis)')
0
+ def add_postamble(src)
0
+ src << "\n" unless src[-1] == ?\n
0
+ src << "_ret = @_erb_buf; @_erb_buf = _old_buf; _ret.to_s\n"
0
+ end
0
+
0
+ def add_text(src, text)
0
+ src << " @_erb_buf << '" << escape_text(text) << "'; "
0
+ end
0
+
0
+ def add_expr_escaped(src, code)
0
+ src << ' @_erb_buf << ' << escaped_expr(code) << ';'
0
+ end
0
+
0
+ def add_expr_literal(src, code)
0
+ unless code =~ /(do|\{)(\s*|[^|]*|)?\s*$/
0
+ src << ' @_erb_buf << (' << code << ').to_s;'
0
+ else
0
+ src << ' @_erb_buf << ' << code << "; "
0
+ end
0
     end
0
-
0
   end
0
+
0
+ class BlockAwareEruby < Eruby
0
+ include BlockAwareEnhancer
0
+ end
0
+
0
+ # module RubyEvaluator
0
+ #
0
+ # # DOC
0
+ # def def_method(object, method_name, filename=nil)
0
+ # m = object.is_a?(Module) ? :module_eval : :instance_eval
0
+ # setup = "@_engine = 'erb'"
0
+ # object.__send__(m, "def #{method_name}(locals={}); #{setup}; #{@src}; end", filename || @filename || '(erubis)')
0
+ # end
0
+ #
0
+ # end
0
 end
0
\ No newline at end of file
...
7
8
9
 
 
 
 
10
11
12
13
14
 
 
 
 
 
15
16
17
...
7
8
9
10
11
12
13
14
15
16
 
 
17
18
19
20
21
22
23
24
0
@@ -7,11 +7,18 @@ module Merb::Test::Fixtures
0
       def _template_location(context, type = nil, controller = controller_name)
0
         "helpers/#{File.basename(controller)}/#{context}"
0
       end
0
+
0
+ def index
0
+ render
0
+ end
0
     end
0
     
0
     class Capture < HelperTesting
0
- def index
0
- render
0
+ end
0
+
0
+ class CaptureEq < HelperTesting
0
+ def helper_using_capture(&blk)
0
+ "Beginning... #{capture(&blk)}... Done"
0
       end
0
     end
0
 
...
5
6
7
 
 
 
 
8
9
10
11
12
 
13
...
5
6
7
8
9
10
11
12
13
14
15
 
16
17
0
@@ -5,9 +5,13 @@ describe Merb::AbstractController, " with capture and concat" do
0
   it "should support capture" do
0
     dispatch_should_make_body("Capture", "Capture")
0
   end
0
+
0
+ it "should support basic helpers that use capture with <%=" do
0
+ dispatch_should_make_body("CaptureEq", "Pre. Beginning... Capturing... Done. Post.")
0
+ end
0
   
0
   it "should support concat" do
0
     dispatch_should_make_body("Concat", "Concat")
0
   end
0
-
0
+
0
 end

Comments

    No one has commented yet.