Class: Tilt::ERBTemplate

Inherits:
Template show all
Defined in:
lib/tilt/erb.rb

Overview

Direct Known Subclasses

ErubisTemplate

Constant Summary collapse

SUPPORTS_KVARGS =
::ERB.instance_method(:initialize).parameters.assoc(:key) rescue false
@@default_output_variable =
'_erbout'

Instance Attribute Summary

Attributes inherited from Template

#compiled_path, #data, #file, #line, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Template

#basename, default_mime_type, default_mime_type=, #eval_file, #initialize, metadata, #metadata, #name, #render

Constructor Details

This class inherits a constructor from Tilt::Template

Class Method Details

.default_output_variableObject



12
13
14
# File 'lib/tilt/erb.rb', line 12

def self.default_output_variable
  @@default_output_variable
end

.default_output_variable=(name) ⇒ Object



16
17
18
19
# File 'lib/tilt/erb.rb', line 16

def self.default_output_variable=(name)
  warn "#{self}.default_output_variable= has been replaced with the :outvar-option"
  @@default_output_variable = name
end

Instance Method Details

#freeze_string_literals?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/tilt/erb.rb', line 68

def freeze_string_literals?
  @freeze_string_literals
end

#precompiled(locals) ⇒ Object

ERB generates a line to specify the character coding of the generated source in 1.9. Account for this in the line offset.



63
64
65
66
# File 'lib/tilt/erb.rb', line 63

def precompiled(locals)
  source, offset = super
  [source, offset + 1]
end

#precompiled_postamble(locals) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/tilt/erb.rb', line 52

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

#precompiled_preamble(locals) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/tilt/erb.rb', line 44

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

#precompiled_template(locals) ⇒ Object



39
40
41
42
# File 'lib/tilt/erb.rb', line 39

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

#prepareObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tilt/erb.rb', line 21

def prepare
  @freeze_string_literals = !!@options[:freeze]
  @outvar = options[:outvar] || self.class.default_output_variable
  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)
  else
    ::ERB.new(data, options[:safe], trim, @outvar)
  end
end