Sha256: ba33071773ed4dea02b95703a6ed5595274dee16e796853334797140cf598787

Contents?: true

Size: 1.33 KB

Versions: 31

Compression:

Stored size: 1.33 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # 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.
    module CaptureERB
      def self.load_dependencies(app)
        app.plugin :render
      end

      module InstanceMethods
        # 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.
        def capture_erb
          outvar = render_opts[:template_opts][:outvar]
          buf_was = instance_variable_get(outvar)
          instance_variable_set(outvar, String.new)
          yield.to_s
        ensure
          instance_variable_set(outvar, buf_was) if outvar && buf_was
        end
      end
    end

    register_plugin(:capture_erb, CaptureERB)
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
roda-3.81.0 lib/roda/plugins/capture_erb.rb
roda-3.79.0 lib/roda/plugins/capture_erb.rb
roda-3.78.0 lib/roda/plugins/capture_erb.rb
roda-3.77.0 lib/roda/plugins/capture_erb.rb
roda-3.76.0 lib/roda/plugins/capture_erb.rb
roda-3.75.0 lib/roda/plugins/capture_erb.rb
roda-3.74.0 lib/roda/plugins/capture_erb.rb
roda-3.73.0 lib/roda/plugins/capture_erb.rb
roda-3.72.0 lib/roda/plugins/capture_erb.rb
roda-3.71.0 lib/roda/plugins/capture_erb.rb
roda-3.70.0 lib/roda/plugins/capture_erb.rb
roda-3.69.0 lib/roda/plugins/capture_erb.rb
roda-3.68.0 lib/roda/plugins/capture_erb.rb
roda-3.67.0 lib/roda/plugins/capture_erb.rb
roda-3.66.0 lib/roda/plugins/capture_erb.rb
roda-3.65.0 lib/roda/plugins/capture_erb.rb
roda-3.64.0 lib/roda/plugins/capture_erb.rb
roda-3.63.0 lib/roda/plugins/capture_erb.rb
roda-3.62.0 lib/roda/plugins/capture_erb.rb
roda-3.61.0 lib/roda/plugins/capture_erb.rb