Class: Tilt::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/frank/tilt.rb

Overview

Base class for template implementations. Subclasses must implement the #prepare method and one of the #evaluate or #template_source 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)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/frank/tilt.rb', line 93

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
    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 generate unique method names for template compilation
  @stamp = (Time.now.to_f * 10000).to_i
  @compiled_method_names = {}

  # load template data and prepare
  if @file.match(/^[^\n]+$/) && File.exist?(@file)
    @reader = block || lambda { |t| File.read(@file) }
  else
    @reader = block || lambda { |t| @file }
  end
  
  @data = @reader.call(self)
  prepare
end

Class Attribute Details

.engine_initializedObject Also known as: engine_initialized?

Returns the value of attribute engine_initialized.



83
84
85
# File 'lib/frank/tilt.rb', line 83

def engine_initialized
  @engine_initialized
end

Instance Attribute Details

#dataObject (readonly)

Template source; loaded from a file or given directly.



66
67
68
# File 'lib/frank/tilt.rb', line 66

def data
  @data
end

#fileObject (readonly)

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



69
70
71
# File 'lib/frank/tilt.rb', line 69

def file
  @file
end

#lineObject (readonly)

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



72
73
74
# File 'lib/frank/tilt.rb', line 72

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.



77
78
79
# File 'lib/frank/tilt.rb', line 77

def options
  @options
end

Instance Method Details

#basename(suffix = '') ⇒ Object

The basename of the template file.



137
138
139
# File 'lib/frank/tilt.rb', line 137

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

#eval_fileObject

The filename used in backtraces to describe the template.



147
148
149
# File 'lib/frank/tilt.rb', line 147

def eval_file
  file || '(__TEMPLATE__)'
end

#nameObject

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



142
143
144
# File 'lib/frank/tilt.rb', line 142

def name
  basename.split('.', 2).first if basename
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.



132
133
134
# File 'lib/frank/tilt.rb', line 132

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