lib/dry/view/renderer.rb in dry-view-0.3.0 vs lib/dry/view/renderer.rb in dry-view-0.4.0

- old
+ new

@@ -2,10 +2,13 @@ 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 @@ -18,21 +21,25 @@ @paths = paths @format = format @tilts = self.class.tilts end - def call(template, scope, &block) - path = lookup(template) + def template(name, scope, &block) + path = lookup(name) if path render(path, scope, &block) else - msg = "Template #{template.inspect} could not be found in paths:\n#{paths.map { |pa| "- #{pa.to_s}" }.join("\n")}" + 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) @@ -46,9 +53,14 @@ 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")