module Roda::RodaPlugins::CaptureERB::InstanceMethods

  1. lib/roda/plugins/capture_erb.rb

Methods

Public Instance

  1. capture_erb

Public Instance methods

capture_erb(opts=(no_positional_opts = true; OPTS), returns: (opts.has_key?(:returns) ? opts[:returns] : (returns_not_set = true; nil)), **kw, &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.

Keyword arguments:

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
72 def capture_erb(opts=(no_positional_opts = true; OPTS), returns: (opts.has_key?(:returns) ? opts[:returns] : (returns_not_set = true; nil)), **kw, &block)
73   RodaPlugins.check_keywords(__callee__, no_positional_opts, opts, kw)
74 
75   outvar = render_opts[:template_opts][:outvar]
76   buf_was = instance_variable_get(outvar)
77 
78   if buf_was.respond_to?(:capture) && !buf_was.instance_of?(String)
79     buf_was.capture(&block)
80   else
81     returns = self.class.capture_erb_returns if returns_not_set
82 
83     begin
84       instance_variable_set(outvar, String.new)
85       if returns == :buffer
86         yield
87         instance_variable_get(outvar).to_s
88       else
89         yield.to_s
90       end
91     ensure
92       instance_variable_set(outvar, buf_was) if outvar && buf_was
93     end
94   end
95 end