Class: Tilt::Template

Inherits:
Object show all
Defined in:
lib/vendor/tilt-1.4.1/lib/tilt/template.rb

Overview

Base class for template implementations. Subclasses must implement the #prepare method and one of the #evaluate or #precompiled_template methods.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, line = 1, options = {}, &block) ⇒ Template

Create a new template with the file, line, and options specified. By default, template data is read from the file. When a block is given, it should read template data and return as a String. When file is nil, a block is required.

All arguments are optional.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 38

def initialize(file=nil, line=1, options={}, &block)
  @file, @line, @options = nil, 1, {}

  [options, line, file].compact.each do |arg|
    case
    when arg.respond_to?(:to_str)  ; @file = arg.to_str
    when arg.respond_to?(:to_int)  ; @line = arg.to_int
    when arg.respond_to?(:to_hash) ; @options = arg.to_hash.dup
    when arg.respond_to?(:path)    ; @file = arg.path
    else raise TypeError
    end
  end

  raise ArgumentError, "file or block required" if (@file || block).nil?

  # call the initialize_engine method if this is the very first time
  # an instance of this class has been created.
  if !self.class.engine_initialized?
    initialize_engine
    self.class.engine_initialized = true
  end

  # used to hold compiled template methods
  @compiled_method = {}

  # used on 1.9 to set the encoding if it is not set elsewhere (like a magic comment)
  # currently only used if template compiles to ruby
  @default_encoding = @options.delete :default_encoding

  # load template data and prepare (uses binread to avoid encoding issues)
  @reader = block || lambda { |t| read_template_file }
  @data = @reader.call(self)

  if @data.respond_to?(:force_encoding)
    @data.force_encoding(default_encoding) if default_encoding

    if !@data.valid_encoding?
      raise Encoding::InvalidByteSequenceError, "#{eval_file} is not valid #{@data.encoding}"
    end
  end

  prepare
end

Class Attribute Details

.default_mime_typeObject

Returns the value of attribute default_mime_type.



29
30
31
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 29

def default_mime_type
  @default_mime_type
end

.engine_initializedObject Also known as: engine_initialized?

Returns the value of attribute engine_initialized.



26
27
28
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 26

def engine_initialized
  @engine_initialized
end

Instance Attribute Details

#dataObject (readonly)

Template source; loaded from a file or given directly.



9
10
11
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 9

def data
  @data
end

#fileObject (readonly)

The name of the file where the template data was loaded from.



12
13
14
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 12

def file
  @file
end

#lineObject (readonly)

The line number in #file where template data was loaded from.



15
16
17
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 15

def line
  @line
end

#optionsObject (readonly)

A Hash of template engine specific options. This is passed directly to the underlying engine and is not used by the generic template interface.



20
21
22
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 20

def options
  @options
end

Instance Method Details

#allows_script?Boolean

Whether or not this template engine allows executing Ruby script within the template. If this is false, scope and locals will generally not be used, nor will the provided block be avaiable via yield. This should be overridden by template subclasses.

Returns:

  • (Boolean)


126
127
128
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 126

def allows_script?
  true
end

#basename(suffix = '') ⇒ Object

The basename of the template file.



107
108
109
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 107

def basename(suffix='')
  File.basename(file, suffix) if file
end

#default_encodingObject

The encoding of the source data. Defaults to the default_encoding-option if present. You may override this method in your template class if you have a better hint of the data’s encoding.



86
87
88
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 86

def default_encoding
  @default_encoding
end

#eval_fileObject

The filename used in backtraces to describe the template.



117
118
119
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 117

def eval_file
  file || '(__TEMPLATE__)'
end

#nameObject

The template file’s basename with all extensions chomped off.



112
113
114
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 112

def name
  basename.split('.', 2).first if basename
end

#read_template_fileObject



90
91
92
93
94
95
96
97
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 90

def read_template_file
  data = File.open(file, 'rb') { |io| io.read }
  if data.respond_to?(:force_encoding)
    # Set it to the default external (without verifying)
    data.force_encoding(Encoding.default_external) if Encoding.default_external
  end
  data
end

#render(scope = Object.new, locals = {}, &block) ⇒ Object

Render the template in the given scope with the locals specified. If a block is given, it is typically available within the template via yield.



102
103
104
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 102

def render(scope=Object.new, locals={}, &block)
  evaluate scope, locals || {}, &block
end