Module: Sinatra::Capture

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

Overview

Sinatra::Capture

Extension that enables blocks inside other extensions. It currently works for erb, slim and haml. Enables mixing of different template languages.

Example:

# in hello_world.erb

Say
<% a = capture do %>World<% end %>
Hello <%= a %>!

# in hello_world.slim

| Say
- a = capture do
  | World
|  Hello #{a}!

# in hello_world.haml

Say
- a = capture do
  World
  Hello #{a.strip}!

You can also use nested blocks.

Example

# in hello_world.erb

Say
<% a = capture do %>
  <% b = capture do %>World<% end %>
    <%= b %>!
<% end %>
Hello <%= a.strip %>

The main advantage of capture is mixing of different template engines.

Example

# in mix_me_up.slim

- two = capture do
  - erb "<%= 1 + 1 %>"
| 1 + 1 = #{two}

Usage

Classic Application

In a classic application simply require the helpers, and start using them:

require "sinatra"
require "sinatra/capture"

# The rest of your classic application code goes here...

Modular Application

In a modular application you need to require the helpers, and then tell the application you will use them:

require "sinatra/base"
require "sinatra/capture"

class MyApp < Sinatra::Base
  helpers Sinatra::Capture

  # The rest of your modular application code goes here...
end

Constant Summary collapse

DUMMIES =
{
  :haml   => "!= capture_haml(*args, &block)",
  :erubis => "<% @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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sinatra/capture.rb', line 94

def capture(*args, &block)
  @capture = nil
  if current_engine == :ruby
    result = block[*args]
  elsif current_engine == :erb || current_engine == :slim
    @_out_buf, _buf_was = '', @_out_buf
    block[*args]
    result = eval('@_out_buf', block.binding)
    @_out_buf = _buf_was
  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



117
118
119
120
# File 'lib/sinatra/capture.rb', line 117

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