Module: Roda::RodaPlugins::CaptureERB
- Defined in:
- lib/roda/plugins/capture_erb.rb
Overview
The capture_erb plugin allows you to capture the content of a block in an ERB template, and return it as a value, instead of injecting the template block into the template output.
<% value = capture_erb do %>
Some content here.
<% end %>
capture_erb can be used inside other methods that are called
inside templates. It can be combined with the inject_erb plugin
to wrap template blocks with arbitrary output and then inject the
wrapped output into the template.
If the output buffer object responds to capture and is not
an instance of String (e.g. when erubi/capture_block is being
used as the template engine), this will call capture on the
output buffer object, instead of setting the output buffer object
temporarily to a new object.
By default, capture_erb returns the value of the block, converted to a string. However, that can cause issues with code such as:
<% value = capture_erb do %>
Some content here.
<% if something %>
Some more content here.
<% end %>
<% end %>
In this case, the block may return nil, instead of the content of
the template. To handle this case, you can provide the
returns: :buffer option when calling the method (to handle
that specific call, or when loading the plugin (to default to that
behavior). Note that if the output buffer object responds to
capture and is not an instance of String, the returns: :buffer
behavior is the default and cannot be changed.
Defined Under Namespace
Modules: InstanceMethods
Class Method Summary collapse
-
.configure(app, opts = OPTS) ⇒ Object
Support returns: :buffer to default to returning buffer object.
- .load_dependencies(app, opts = OPTS) ⇒ Object
Class Method Details
.configure(app, opts = OPTS) ⇒ Object
Support returns: :buffer to default to returning buffer object.
49 50 51 52 |
# File 'lib/roda/plugins/capture_erb.rb', line 49 def self.configure(app, opts=OPTS) # RODA4: make returns: :buffer the default behavior app.opts[:capture_erb_returns] = opts[:returns] if opts.has_key?(:returns) end |
.load_dependencies(app, opts = OPTS) ⇒ Object
43 44 45 |
# File 'lib/roda/plugins/capture_erb.rb', line 43 def self.load_dependencies(app, opts=OPTS) app.plugin :render end |