Skip to content

Commit

Permalink
Try replacing _erbout with @output_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Jun 3, 2008
1 parent da91450 commit 933697a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 54 deletions.
5 changes: 1 addition & 4 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -178,10 +178,7 @@ def self.cache_template_extensions=(*args)
# that alert()s the caught exception (and then re-raises it).
@@debug_rjs = false
cattr_accessor :debug_rjs

@@erb_variable = '_erbout'
cattr_accessor :erb_variable


attr_internal :request

delegate :request_forgery_protection_token, :template, :params, :session, :cookies, :response, :headers,
Expand Down
56 changes: 14 additions & 42 deletions actionpack/lib/action_view/helpers/capture_helper.rb
Expand Up @@ -31,20 +31,13 @@ module CaptureHelper
# </body></html>
#
def capture(*args, &block)
# execute the block
begin
buffer = eval(ActionView::Base.erb_variable, block.binding)
rescue
buffer = nil
end

if buffer.nil?
capture_block(*args, &block).to_s
else
capture_erb_with_buffer(buffer, *args, &block).to_s
end
@output_buffer, old_buffer = '', @output_buffer
yield *args
@output_buffer
ensure
@output_buffer = old_buffer
end

# Calling content_for stores a block of markup in an identifier for later use.
# You can make subsequent calls to the stored content in other templates or the layout
# by passing the identifier as an argument to <tt>yield</tt>.
Expand Down Expand Up @@ -121,40 +114,19 @@ def capture(*args, &block)
# named <tt>@content_for_#{name_of_the_content_block}</tt>. The preferred usage is now
# <tt><%= yield :footer %></tt>.
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)
ivar = "@content_for_#{name}"
instance_variable_set("@content_for_#{name}", "#{instance_variable_get(ivar)}#{block_given? ? capture(&block) : content}")
end

private
def capture_block(*args, &block)
block.call(*args)
end

def capture_erb(*args, &block)
buffer = eval(ActionView::Base.erb_variable, block.binding)
capture_erb_with_buffer(buffer, *args, &block)
end

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

def erb_content_for(name, &block)
eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_erb(&block)"
ivar = "@content_for_#{name}"
instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{capture(&block)}")
end

def block_content_for(name, &block)
eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_block(&block)"

def block_content_for(name)
ivar = "@content_for_#{name}"
instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{yield}")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/javascript_helper.rb
Expand Up @@ -208,7 +208,7 @@ def array_or_string_for_javascript(option)

private
def block_is_within_action_view?(block)
eval("defined? _erbout", block.binding)
!@output_buffer.nil?
end
end

Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/tag_helper.rb
Expand Up @@ -126,7 +126,7 @@ def tag_options(options, escape = true)
end

def block_is_within_action_view?(block)
eval("defined? _erbout", block.binding)
!@output_buffer.nil?
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions actionpack/lib/action_view/helpers/text_helper.rb
Expand Up @@ -25,8 +25,12 @@ module TextHelper
# end
# # will either display "Logged in!" or a login link
# %>
def concat(string, binding)
eval(ActionView::Base.erb_variable, binding) << string
def concat(string, binding = nil)
if @output_buffer
@output_buffer << string
else
string
end
end

if RUBY_VERSION < '1.9'
Expand Down
6 changes: 2 additions & 4 deletions actionpack/lib/action_view/template_handlers/erb.rb
Expand Up @@ -43,13 +43,11 @@ class ERB < TemplateHandler
include Compilable

def compile(template)
::ERB.new(template.source, nil, @view.erb_trim_mode).src
::ERB.new(template.source, nil, @view.erb_trim_mode, '@output_buffer').src
end

def cache_fragment(block, name = {}, options = nil) #:nodoc:
@view.fragment_for(block, name, options) do
eval(ActionView::Base.erb_variable, block.binding)
end
@view.fragment_for(block, name, options) { @view.output_buffer }
end
end
end
Expand Down

0 comments on commit 933697a

Please sign in to comment.