Module: Sinatra::Capture
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
Instance Attribute Summary
Attributes included from EngineTracking
Instance Method Summary collapse
Methods included from EngineTracking
#builder?, #erb?, #erubi?, #haml?, #initialize, #liquid?, #markaby?, #markdown?, #nokogiri?, #rdoc?, #ruby?, #sass?, #scss?, #slim?, #with_engine
Instance Method Details
#capture(*args, &block) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/sinatra/capture.rb', line 87 def capture(*args, &block) return block[*args] if ruby? if haml? && Tilt[:haml] == Tilt::HamlTemplate && defined?(Haml::Buffer) buffer = Haml::Buffer.new(nil, Haml::Options.new.for_buffer) with_haml_buffer(buffer) { capture_haml(*args, &block) } else buf_was = @_out_buf @_out_buf = +'' begin raw = block[*args] captured = block.binding.eval('@_out_buf') captured.empty? ? raw : captured ensure @_out_buf = buf_was end end end |
#capture_later(&block) ⇒ Object
106 107 108 109 |
# File 'lib/sinatra/capture.rb', line 106 def capture_later(&block) engine = current_engine proc { |*a| with_engine(engine) { @capture = capture(*a, &block) } } end |