Module: Sinatra::Capture

Includes:
EngineTracking
Included in:
ContentFor
Defined in:
lib/sinatra/capture.rb

Constant Summary collapse

DUMMIES =
{
  :haml => "!= capture_haml(*args, &block)",
  :erb  => "<% @capture = yield(*args) %>",
  :slim => "== yield(*args)"
}

Instance Attribute Summary

Attributes included from EngineTracking

#current_engine

Instance Method Summary collapse

Methods included from EngineTracking

#builder?, #coffee?, #creole?, #erb?, #erubis?, #haml?, #initialize, #less?, #liquid?, #markaby?, #markdown?, #nokogiri?, #radius?, #rdoc?, #sass?, #scss?, #slim?, #textile?, #with_engine

Instance Method Details

#capture(*args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sinatra/capture.rb', line 17

def capture(*args, &block)
  @capture = nil
  if current_engine == :ruby
    result = block[*args]
  else
    buffer     = eval '_buf if defined?(_buf)', block.binding
    old_buffer = buffer.dup if buffer
    dummy      = DUMMIES.fetch(current_engine)
    options    = { :layout => false, :locals => {:args => args, :block => block }}

    buffer.try :clear
    result = render(current_engine, dummy, options, &block)
  end
  result.strip.empty? && @capture ? @capture : result
ensure
  buffer.try :replace, old_buffer
end

#capture_later(&block) ⇒ Object



35
36
37
38
# File 'lib/sinatra/capture.rb', line 35

def capture_later(&block)
  engine = current_engine
  proc { |*a| with_engine(engine) { @capture = capture(*a, &block) }}
end