/
templates.cr
68 lines (57 loc) · 2.58 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module Marten
module Conf
class GlobalSettings
# Allows to configure templates-related settings.
class Templates
@app_dirs : Bool = true
@cached : Bool = false
@context_producers = [] of Template::ContextProducer.class
@dirs : Array(String) = [] of String
@strict_variables : Bool = false
# Returns a boolean indicating whether templates should be looked for inside installed applications.
getter app_dirs
# Returns a boolean indicating whether templates should be kept in a memory cache upon being loaded and parsed.
getter cached
# Returns an array of context producers that will be used to populate the context for each template.
getter context_producers
# Returns an array of directories where templates should be looked for.
#
# The order of these directories is important as it defines the order in which templates are searched for.
getter dirs
# Returns `true` if the strict variables mode is enabled.
getter strict_variables
# :ditto:
getter? strict_variables
# Allows to set whether templates should be looked for inside installed applications.
setter app_dirs
# Allows to set whether templates should be kept in a memory cache upon being loaded and parsed.
#
# By default templates will be read from the filesystem and parsed every time they need to be rendered. When
# setting this configuration option to `true`, compiled templates will be kept in memory and further renderings
# of the same templates will result in previous compiled templates to be reused.
setter cached
# Alows to enable or disable the strict variables mode.
#
# When this setting is set to `true`, unknown variables encountered in templates will result in
# `Marten::Template::Errors::UnknownVariable` exceptions to be raised.
setter strict_variables
# Allows to set the context producers that will be used to populate the context for each template.
def context_producers=(v)
@context_producers = Array(Template::ContextProducer.class).new
@context_producers.concat(v)
end
# Allows to set the directories where templates should be looked for.
def dirs=(dirs : Array(Path | String | Symbol))
@dirs = dirs.map do |dir|
case dir
when Path
dir.expand.to_s
else
dir.to_s
end
end
end
end
end
end
end