class Racket::Utils::Views::TemplateLocator

Class used for locating templates. This class uses the TemplateResolver class internally for getting the template for the first time and caches the result so that subsequent Calls will not need to resolve the template again.

Public Class Methods

get_template(cache, resolver, controller) click to toggle source

Tries to locate a template matching the controllers action in the file system and returns the path if a matching file is found. If no matching file is found, nil is returned. The result is cached, meaning that the filesystem lookup for a specific path will only happen once.

@param [TemplateCache] cache @param [TemplateResolver] resolver @param [Racket::Controller] controller @return [String|nil]

# File lib/racket/utils/views/template_locator.rb, line 72
def self.get_template(cache, resolver, controller)
  path = TemplateResolver.get_template_path(controller)
  unless cache.key?(path)
    template = resolver.get_template_object(path, controller)
    cache.store(path, template)
  end
  resolver.resolve_template(path, cache.load(path), controller)
end
new(options) click to toggle source
# File lib/racket/utils/views/template_locator.rb, line 41
def initialize(options)
  options.each_pair { |key, value| instance_variable_set("@#{key}".to_sym, value) }
end
service(_options = {}) click to toggle source

Returns a service proc that can be used by the registry.

@param [Hash] _options (unused) @return [Proc]

# File lib/racket/utils/views/template_locator.rb, line 30
def self.service(_options = {})
  lambda do |reg|
    new(
      layout_cache: reg.layout_cache,
      layout_resolver: reg.layout_resolver,
      view_cache: reg.view_cache,
      view_resolver: reg.view_resolver
    )
  end
end

Public Instance Methods

get_layout(controller) click to toggle source

Returns the layout associated with the current request. On the first request to any action the result is cached, meaning that the layout only needs to be looked up once.

@param [Racket::Controller] controller @return [String|nil]

# File lib/racket/utils/views/template_locator.rb, line 50
def get_layout(controller)
  self.class.get_template(@layout_cache, @layout_resolver, controller)
end
get_view(controller) click to toggle source

Returns the view associated with the current request. On the first request to any action the result is cached, meaning that the view only needs to be looked up once.

@param [Racket::Controller] controller @return [String|nil]

# File lib/racket/utils/views/template_locator.rb, line 59
def get_view(controller)
  self.class.get_template(@view_cache, @view_resolver, controller)
end