Sha256: c43950bbd3a7ed182d2dae8049cafdd4d43c25bf9ef08f7ce7dfcc7ed2acdb8a

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Hanami
  class View
    module ERB
      module Filters
        # Implicitly captures and outputs the content inside blocks opened in ERB expression tags,
        # such as `<%= form_for(:post) do %>`.
        #
        # Inspired by Slim's Slim::Controls::Filter#on_slim_output.
        #
        # @since 2.1.0
        # @api private
        class Block < Temple::Filter
          END_LINE_RE = /\bend\b/

          def on_erb_block(escape, code, content)
            tmp = unique_name

            # Remove the last `end` :code sexp, since this is technically "outside" the block
            # contents, which we want to capture separately below. This `end` is added back after
            # capturing the content below.
            case content.last
            in [:code, c] if c =~ END_LINE_RE
              content.pop
            end

            [:multi,
              # Capture the result of the code in a variable. We can't do `[:dynamic, code]` because
              # it's probably not a complete expression (which is a requirement for Temple).
              [:code, "#{tmp} = #{code}"],
              # Capture the content of a block in a separate buffer. This means that `yield` will
              # not output the content to the current buffer, but rather return the output.
              [:capture, unique_name, compile(content)],
              [:code, "end"],
              # Output the content.
              [:escape, escape, [:dynamic, tmp]]
            ]
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hanami-view-2.2.0 lib/hanami/view/erb/filters/block.rb
hanami-view-2.2.0.rc1 lib/hanami/view/erb/filters/block.rb
hanami-view-2.2.0.beta2 lib/hanami/view/erb/filters/block.rb
hanami-view-2.2.0.beta1 lib/hanami/view/erb/filters/block.rb
hanami-view-2.1.0 lib/hanami/view/erb/filters/block.rb
hanami-view-2.1.0.rc3 lib/hanami/view/erb/filters/block.rb
hanami-view-2.1.0.rc2 lib/hanami/view/erb/filters/block.rb