Class: Tilt::Template

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

Overview

Base class for template implementations. Subclasses must implement the #compile! 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 specified. When a block is given, it should read template data and return as a String. When file is nil, a block is required.

The #initialize_engine method is called if this is the very first time this template subclass has been initialized.

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sinatra/tilt.rb', line 73

def initialize(file=nil, line=1, options={}, &block)
  raise ArgumentError, "file or block required" if file.nil? && block.nil?
  options, line = line, 1 if line.is_a?(Hash)
  @file = file
  @line = line || 1
  @options = options || {}
  @reader = block || lambda { |t| File.read(file) }

  if !self.class.engine_initialized
    initialize_engine
    self.class.engine_initialized = true
  end
end

Class Attribute Details

.engine_initializedObject

Returns the value of attribute engine_initialized.



93
94
95
# File 'lib/sinatra/tilt.rb', line 93

def engine_initialized
  @engine_initialized
end

Instance Attribute Details

#dataObject (readonly)

Template source; loaded from a file or given directly.



53
54
55
# File 'lib/sinatra/tilt.rb', line 53

def data
  @data
end

#fileObject (readonly)

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



56
57
58
# File 'lib/sinatra/tilt.rb', line 56

def file
  @file
end

#lineObject (readonly)

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



59
60
61
# File 'lib/sinatra/tilt.rb', line 59

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.



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

def options
  @options
end

Instance Method Details

#basename(suffix = '') ⇒ Object

The basename of the template file.



115
116
117
# File 'lib/sinatra/tilt.rb', line 115

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

#compileObject

Load template source and compile the template. The template is loaded and compiled the first time this method is called; subsequent calls are no-ops.



99
100
101
102
103
104
# File 'lib/sinatra/tilt.rb', line 99

def compile
  if @data.nil?
    @data = @reader.call(self)
    compile!
  end
end

#eval_fileObject

The filename used in backtraces to describe the template.



125
126
127
# File 'lib/sinatra/tilt.rb', line 125

def eval_file
  file || '(__TEMPLATE__)'
end

#initialize_engineObject

Called once and only once for each template subclass the first time the template class is initialized. This should be used to require the underlying template library and perform any initial setup.



90
91
# File 'lib/sinatra/tilt.rb', line 90

def initialize_engine
end

#nameObject

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



120
121
122
# File 'lib/sinatra/tilt.rb', line 120

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.



109
110
111
112
# File 'lib/sinatra/tilt.rb', line 109

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