module Angelo::Templates::TemplateCaching

Public Instance Methods

get_template(*args) click to toggle source
# File lib/angelo/templates.rb, line 112
def get_template(*args)
  template_cache.fetch(*args) do
    instantiate_template(*args)
  end
end

Private Instance Methods

find_view_file_for_template_type(views, name, template_type) click to toggle source
# File lib/angelo/templates.rb, line 157
def find_view_file_for_template_type(views, name, template_type)
  template_class = Tilt[template_type]
  exts = Tilt.default_mapping.extensions_for(template_class).uniq
  filenames = exts.map{|ext| File.join(views, "#{name}.#{ext}")}
  filenames.find{|f| File.exists?(f)}
end
instantiate_template(template_type, view, views_dir, opts) click to toggle source
# File lib/angelo/templates.rb, line 124
def instantiate_template(template_type, view, views_dir, opts)
  case view
  when Symbol
    instantiate_template_from_file(template_type, view.to_s, views_dir, opts)
  when String
    instantiate_template_from_string(template_type, view, opts)
  else
    raise ArgumentError, "view must be a Symbol or a String"
  end
end
instantiate_template_from_file(template_type, view, views_dir, opts) click to toggle source
# File lib/angelo/templates.rb, line 135
def instantiate_template_from_file(template_type, view, views_dir, opts)
  # Find a file in the views directory with a correct extension
  # for this template_type.
  file = find_view_file_for_template_type(views_dir, view, template_type)
  if file
    # Use absolute filenames so backtraces have absolute filenames.
    absolute_file = File.expand_path(file)
    Tilt.new(absolute_file, opts)
  end
end
instantiate_template_from_string(template_type, string, opts) click to toggle source
# File lib/angelo/templates.rb, line 146
def instantiate_template_from_string(template_type, string, opts)
  # To make Tilt use a String as a template we pass Tilt.new a
  # block that returns the String.  Passing a block makes
  # Tilt.new ignore the file argument.  Except the file argument
  # is still used to figure out which template engine to use.
  # Fortunately we can just pass template_type, except the file
  # argument needs to respond to to_str and symbols don't
  # respond to that so we convert it to a String.
  Tilt.new(template_type.to_s, opts) {string}
end
template_cache() click to toggle source
# File lib/angelo/templates.rb, line 120
def template_cache
  @reload_templates ? @@non_cache : @@template_cache
end