module Roda::RodaPlugins::CaptureERB::InstanceMethods

  1. lib/roda/plugins/capture_erb.rb

Methods

Public Instance

  1. capture_erb

Public Instance methods

capture_erb(opts=OPTS, &block)

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.

[show source]
   # File lib/roda/plugins/capture_erb.rb
71 def capture_erb(opts=OPTS, &block)
72   outvar = render_opts[:template_opts][:outvar]
73   buf_was = instance_variable_get(outvar)
74 
75   if buf_was.respond_to?(:capture) && !buf_was.instance_of?(String)
76     buf_was.capture(&block)
77   else
78     returns = opts.fetch(:returns) { self.class.capture_erb_returns }
79 
80     begin
81       instance_variable_set(outvar, String.new)
82       if returns == :buffer
83         yield
84         instance_variable_get(outvar).to_s
85       else
86         yield.to_s
87       end
88     ensure
89       instance_variable_set(outvar, buf_was) if outvar && buf_was
90     end
91   end
92 end