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