Sha256: 5574741f7c4d89631eed37417566bdd1652064bfe845936fde133ed8b8a4cd99

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

require_relative "base_renderer.rb"
require_relative "common_features/helperable.rb"
require_relative "common_features/capturable.rb"

module Docks
  module Renderers
    class ERB < Base
      include Common::Helperable
      include Common::Capturable

      def initialize
        require "erb"

        super
        @locals = []
        @output = ""
      end

      def render(template, locals = {})
        first_pass = @output.length < 1
        final_output, @output = @output, ""

        content, layout, locals = normalize_content_and_locals(template, locals)
        return if content.nil?

        @locals << locals
        content = ::ERB.new(content, nil, nil, "@output").result(get_binding)
        return content if layout.nil?

        ::ERB.new(layout, nil, nil, "@output").result(get_binding { |name| name.nil? ? content : @content_blocks[name] })

      ensure
        @locals.pop
        @output = first_pass ? "" : final_output
      end

      def get_binding
        binding
      end

      def capture(*args, &block)
        old_output, @output = @output, ""
        yield *args
        content = @output
        @output = old_output
        content
      end

      def concat(content)
        @output << content
      end

      def method_missing(meth, *arguments, &block)
        super if @locals.empty?
        @locals.last.fetch(meth)
      rescue KeyError
        super
      end

      private

      def clean
        super
        @locals = []
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
docks_app-0.0.3 lib/docks/renderers/erb_renderer.rb
docks_app-0.0.2 lib/docks/renderers/erb_renderer.rb
docks_app-0.0.1 lib/docks/renderers/erb_renderer.rb