Class: Nitro::FileTemplate

Inherits:
OpenStruct
  • Object
show all
Includes:
TemplateMixin
Defined in:
lib/nitro/template.rb

Overview

A Template that reads from files and also provides a simple but effective caching scheme. An intuitive binding mechanism provides the expansion environment. – gmosx: there is a problem with using OpenObject here!!! the eval no longer works! ++

Constant Summary collapse

@@compiled_template_cache =
{}

Constants included from TemplateMixin

TemplateMixin::DELIM_HSH, TemplateMixin::END_DELIM, TemplateMixin::START_DELIM

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TemplateMixin

#compile_template, #evaluate_template, #process_template

Constructor Details

#initialize(filename = nil) ⇒ FileTemplate

Returns a new instance of FileTemplate.



199
200
201
202
# File 'lib/nitro/template.rb', line 199

def initialize(filename = nil)
  super
  @template_filename = filename
end

Instance Attribute Details

#template_filenameObject

Returns the value of attribute template_filename.



197
198
199
# File 'lib/nitro/template.rb', line 197

def template_filename
  @template_filename
end

Instance Method Details

#processObject



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/nitro/template.rb', line 204

def process
  __out__ = ''

  unless compiled = @@compiled_template_cache[@template_filename]
    template = File.read(@template_filename)
    compiled = compile_template(template, '__out__')
    @@compiled_template_cache[@template_filename] = compiled
  end
  
  evaluate_template(compiled, binding)  

  return __out__
end