class Dry::View::Renderer

@api private

Constants

PARTIAL_PREFIX
PATH_DELIMITER

Attributes

engine_mapping[R]
format[R]
options[R]
paths[R]

Public Class Methods

new(paths, format:, engine_mapping: nil, **options) click to toggle source
# File lib/dry/view/renderer.rb, line 21
def initialize(paths, format:, engine_mapping: nil, **options)
  @paths = paths
  @format = format
  @engine_mapping = engine_mapping || {}
  @options = options
end

Public Instance Methods

chdir(dirname) click to toggle source
# File lib/dry/view/renderer.rb, line 52
def chdir(dirname)
  new_paths = paths.map { |path| path.chdir(dirname) }

  self.class.new(new_paths, format: format, **options)
end
partial(name, scope, &block) click to toggle source
# File lib/dry/view/renderer.rb, line 38
def partial(name, scope, &block)
  template(
    name_for_partial(name),
    scope,
    child_dirs: %w[shared],
    parent_dir: true,
    &block
  )
end
render(path, scope, &block) click to toggle source
# File lib/dry/view/renderer.rb, line 48
def render(path, scope, &block)
  tilt(path).render(scope, &block)
end
template(name, scope, **lookup_options, &block) click to toggle source
# File lib/dry/view/renderer.rb, line 28
def template(name, scope, **lookup_options, &block)
  path = lookup(name, **lookup_options)

  if path
    render(path, scope, &block)
  else
    raise TemplateNotFoundError.new(name, paths)
  end
end

Private Instance Methods

lookup(name, **options) click to toggle source
# File lib/dry/view/renderer.rb, line 60
def lookup(name, **options)
  paths.inject(nil) { |_, path|
    result = path.lookup(name, format, **options)
    break result if result
  }
end
name_for_partial(name) click to toggle source
# File lib/dry/view/renderer.rb, line 67
def name_for_partial(name)
  name_segments = name.to_s.split(PATH_DELIMITER)
  name_segments[0..-2].push("#{PARTIAL_PREFIX}#{name_segments[-1]}").join(PATH_DELIMITER)
end
tilt(path) click to toggle source
# File lib/dry/view/renderer.rb, line 72
def tilt(path)
  fetch_or_store(:engine, path, engine_mapping, options) {
    Tilt[path, engine_mapping, **options]
  }
end