-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.cr
53 lines (46 loc) · 1.55 KB
/
templates.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Azu
# Templates are used by Azu when rendering responses.
#
# Since many views render significant content, for example a
# whole HTML file, it is common to put these files into a particular
# directory, typically "src/templates".
#
# This module provides conveniences for reading all files from a particular
# directory and embedding them into a single module. Imagine you have a directory with templates:
#
# Templates::Renderable will define a private function named `render(template : String, data)` with
# one clause per file system template.
#
# ```
# render(template : String, data)
# ```
class Templates
private getter crinja = Crinja.new
getter path : String
getter error_path : String
module Renderable
private def view(template : String = page_path, data = Hash(String, String).new)
CONFIG.templates.load(template).render(data)
end
def page_path
"#{self.class.name.split("::")[1..-1].join("/").underscore.downcase}.jinja"
end
end
def initialize(@path : String, @error_path : String)
crinja.loader = Crinja::Loader::FileSystemLoader.new([path, error_path])
end
def path=(path : String)
reload { @path = Path[path].expand.to_s }
end
def error_path=(path : String)
reload { @error_path = Path[path].expand.to_s }
end
def load(template : String)
crinja.get_template template
end
private def reload
with self yield
crinja.loader = Crinja::Loader::FileSystemLoader.new([path, error_path])
end
end
end