Sha256: 06a4c5a472d97da13da332f7596758e55b6d2f934bd37c3d40024464c643a9cf
Contents?: true
Size: 1.69 KB
Versions: 4
Compression:
Stored size: 1.69 KB
Contents
require 'tilt' require 'dry-equalizer' module Dry module View class Renderer PARTIAL_PREFIX = "_".freeze PATH_DELIMITER = "/".freeze include Dry::Equalizer(:paths, :format) TemplateNotFoundError = Class.new(StandardError) attr_reader :paths, :format, :engine, :tilts def self.tilts @__engines__ ||= {} end def initialize(paths, format:) @paths = paths @format = format @tilts = self.class.tilts end def template(name, scope, &block) path = lookup(name) if path render(path, scope, &block) else msg = "Template #{name.inspect} could not be found in paths:\n#{paths.map { |pa| "- #{pa.to_s}" }.join("\n")}" raise TemplateNotFoundError, msg end end def partial(name, scope, &block) template(name_for_partial(name), scope, &block) end def render(path, scope, &block) tilt(path).render(scope, &block) end def chdir(dirname) new_paths = paths.map { |path| path.chdir(dirname) } self.class.new(new_paths, format: format) end def lookup(name) paths.inject(false) { |result, path| result || path.lookup(name, format) } end private def name_for_partial(name) name_segments = name.to_s.split(PATH_DELIMITER) partial_name = name_segments[0..-2].push("#{PARTIAL_PREFIX}#{name_segments[-1]}").join(PATH_DELIMITER) end # TODO: make default_encoding configurable def tilt(path) tilts.fetch(path) { tilts[path] = Tilt.new(path, nil, default_encoding: "utf-8") } end end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
dry-view-0.5.2 | lib/dry/view/renderer.rb |
dry-view-0.5.1 | lib/dry/view/renderer.rb |
dry-view-0.5.0 | lib/dry/view/renderer.rb |
dry-view-0.4.0 | lib/dry/view/renderer.rb |