Class: Dry::View::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/view/renderer.rb

Constant Summary collapse

TemplateNotFoundError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, options = {}) ⇒ Renderer

Returns a new instance of Renderer.



17
18
19
20
21
22
23
# File 'lib/dry/view/renderer.rb', line 17

def initialize(dir, options = {})
  @dir = dir
  @root = options.fetch(:root, dir)
  @format = options[:format]
  @engine = options[:engine]
  @tilts = self.class.tilts
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



11
12
13
# File 'lib/dry/view/renderer.rb', line 11

def dir
  @dir
end

#engineObject (readonly)

Returns the value of attribute engine.



11
12
13
# File 'lib/dry/view/renderer.rb', line 11

def engine
  @engine
end

#formatObject (readonly)

Returns the value of attribute format.



11
12
13
# File 'lib/dry/view/renderer.rb', line 11

def format
  @format
end

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/dry/view/renderer.rb', line 11

def root
  @root
end

#tiltsObject (readonly)

Returns the value of attribute tilts.



11
12
13
# File 'lib/dry/view/renderer.rb', line 11

def tilts
  @tilts
end

Class Method Details

.tiltsObject



13
14
15
# File 'lib/dry/view/renderer.rb', line 13

def self.tilts
  @__engines__ ||= {}
end

Instance Method Details

#call(template, scope, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/dry/view/renderer.rb', line 25

def call(template, scope, &block)
  path = lookup(template)

  if path
    render(path, scope, &block)
  else
    raise TemplateNotFoundError, "Template #{template} could not be looked up within #{root}"
  end
end

#chdir(dirname) ⇒ Object



63
64
65
# File 'lib/dry/view/renderer.rb', line 63

def chdir(dirname)
  self.class.new(dir.join(dirname), engine: engine, format: format, root: root)
end

#lookup(name) ⇒ Object



43
44
45
# File 'lib/dry/view/renderer.rb', line 43

def lookup(name)
  template?(name) || template?("shared/#{name}") || !root? && chdir('..').lookup(name)
end

#path(name) ⇒ Object



59
60
61
# File 'lib/dry/view/renderer.rb', line 59

def path(name)
  dir.join("#{name}.#{format}.#{engine}")
end

#render(path, scope, &block) ⇒ Object



35
36
37
# File 'lib/dry/view/renderer.rb', line 35

def render(path, scope, &block)
  tilt(path).render(scope, &block)
end

#root?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/dry/view/renderer.rb', line 47

def root?
  dir == root
end

#template?(name) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/dry/view/renderer.rb', line 51

def template?(name)
  template_path = path(name)

  if File.exist?(template_path)
    template_path
  end
end

#tilt(path) ⇒ Object



39
40
41
# File 'lib/dry/view/renderer.rb', line 39

def tilt(path)
  tilts.fetch(path) { tilts[path] = Tilt[engine].new(path, nil, default_encoding: "utf-8") }
end