Sha256: e2c3c780392ff25defc0588adbf3c1bc660ef017d32c5ec936b9709d161a0273

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true
require_relative 'template'
require 'erb'

module Tilt
  # ERB template implementation. See:
  # http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
  class ERBTemplate < Template
    SUPPORTS_KVARGS = ::ERB.instance_method(:initialize).parameters.assoc(:key) rescue false

    def prepare
      @freeze_string_literals = !!@options[:freeze]
      @outvar = @options[:outvar] || '_erbout'
      trim = case @options[:trim]
      when false
        nil
      when nil, true
        '<>'
      else
        @options[:trim]
      end
      @engine = if SUPPORTS_KVARGS
        ::ERB.new(@data, trim_mode: trim, eoutvar: @outvar)
      # :nocov:
      else
        ::ERB.new(@data, options[:safe], trim, @outvar)
      # :nocov:
      end
    end

    def precompiled_template(locals)
      source = @engine.src
      source
    end

    def precompiled_preamble(locals)
      <<-RUBY
        begin
          __original_outvar = #{@outvar} if defined?(#{@outvar})
          #{super}
      RUBY
    end

    def precompiled_postamble(locals)
      <<-RUBY
          #{super}
        ensure
          #{@outvar} = __original_outvar
        end
      RUBY
    end

    # ERB generates a line to specify the character coding of the generated
    # source in 1.9. Account for this in the line offset.
    def precompiled(locals)
      source, offset = super
      [source, offset + 1]
    end

    def freeze_string_literals?
      @freeze_string_literals
    end
  end
end

Version data entries

13 entries across 12 versions & 5 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/tilt-2.4.0/lib/tilt/erb.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/tilt-2.4.0/lib/tilt/erb.rb
tilt-2.6.0 lib/tilt/erb.rb
brakeman-7.0.0 bundle/ruby/3.1.0/gems/tilt-2.5.0/lib/tilt/erb.rb
tilt-2.5.0 lib/tilt/erb.rb
brakeman-6.2.2 bundle/ruby/3.1.0/gems/tilt-2.4.0/lib/tilt/erb.rb
brakeman-6.2.2.rc1 bundle/ruby/3.3.0/gems/tilt-2.4.0/lib/tilt/erb.rb
brakeman-6.2.1 bundle/ruby/3.1.0/gems/tilt-2.4.0/lib/tilt/erb.rb
brakeman-6.2.0 bundle/ruby/3.1.0/gems/tilt-2.4.0/lib/tilt/erb.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/tilt-2.4.0/lib/tilt/erb.rb
tilt-2.4.0 lib/tilt/erb.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/tilt-2.3.0/lib/tilt/erb.rb
tilt-2.3.0 lib/tilt/erb.rb