Module: Roda::RodaPlugins::CaptureERB::InstanceMethods

Defined in:
lib/roda/plugins/capture_erb.rb

Instance Method Summary collapse

Instance Method Details

#capture_erb(opts = OPTS, &block) ⇒ Object

Temporarily replace the ERB output buffer with an empty string, and then yield to the block. Return the value of the block, converted to a string. Restore the previous ERB output buffer before returning.

Options:

:returns

If set to :buffer, returns the value of the template output variable, instead of the return value of the block converted to a string. This is the default behavior if the template output variable supports the capture method and is not a String instance.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/roda/plugins/capture_erb.rb', line 67

def capture_erb(opts=OPTS, &block)
  outvar = render_opts[:template_opts][:outvar]
  buf_was = instance_variable_get(outvar)

  if buf_was.respond_to?(:capture) && !buf_was.instance_of?(String)
    buf_was.capture(&block)
  else
    returns = opts.fetch(:returns) { self.opts[:capture_erb_returns] }

    begin
      instance_variable_set(outvar, String.new)
      if returns == :buffer
        yield
        instance_variable_get(outvar).to_s
      else
        yield.to_s
      end
    ensure
      instance_variable_set(outvar, buf_was) if outvar && buf_was
    end
  end
end