class Flatrack::Template

The default template parser/finder

Constants

DEFAULT_FORMAT

@private

Attributes

base_path[R]
file[R]
format[R]
type[R]

Public Class Methods

find(base_path, type, format, file) click to toggle source

Creates a new template instance and invokes find @param type [Symbol] the type of template @param format [String] the format e.g. html @param file [String] the location of the file

# File lib/flatrack/template.rb, line 26
def self.find(base_path, type, format, file)
  new(base_path, type, format, file)
end
new(base_path, type, format, file) click to toggle source

Creates a new template instance @param type [Symbol] the type of template @param format [String] the format e.g. html @param file [String] the location of the file

# File lib/flatrack/template.rb, line 34
def initialize(base_path, type, format, file)
  @base_path   = base_path
  @format      = format || DEFAULT_FORMAT
  @type, @file = type, file.to_s
  @renderer    = find
end
register_path(path) click to toggle source
# File lib/flatrack/template.rb, line 15
def self.register_path(path)
  classes.each do |klass|
    files = Dir.glob File.join path, '**', "*.#{klass::RENDERS}"
    Tilt.register klass, *files
  end
end

Private Class Methods

classes() click to toggle source
# File lib/flatrack/template.rb, line 43
def self.classes
  constants.map { |k| const_get k }.select do |klass|
    klass.is_a?(Class) && klass < Tilt::Template
  end
end

Private Instance Methods

find() click to toggle source
# File lib/flatrack/template.rb, line 49
def find
  template = find_by_type
  fail FileNotFound, "could not find #{file}" unless template
  template = File.expand_path template
  Tilt.new template, options
rescue RuntimeError
  raise TemplateNotFound, "could not find a renderer for #{file}"
end
find_by_type() click to toggle source
# File lib/flatrack/template.rb, line 65
def find_by_type
  paths = [base_path, Flatrack.gem_root]

  # To support direct html files
  if paths.any? { |path| file.start_with? path } && File.exist?(file)
    file
  else
    file_without_format = File.join(File.dirname(file), File.basename(file, ".#{format}"))
    file_with_format = [file_without_format, format].compact.join('.')
    Dir[File.join base_path, type.to_s.pluralize, "#{file_with_format}*"].first
  end
end
options() click to toggle source
Calls superclass method
# File lib/flatrack/template.rb, line 58
def options
  local_options = {}
  super.merge local_options
rescue NoMethodError
  local_options
end