9
9
/// The statement is for
10
10
public struct IF : GlobalElement {
11
11
12
- public enum IFPrerenderErrors : Error {
13
-
14
- case dynamiclyEvaluatedCondition
15
- }
16
-
17
12
internal class Condition {
18
13
19
14
internal let condition : Conditionable
20
15
21
- internal var formula : Formula
22
-
23
- internal var view : AnyContent
16
+ internal var content : AnyContent
24
17
25
18
internal init ( condition: Conditionable ) {
26
19
27
20
self . condition = condition
28
- self . view = " "
29
- self . formula = Formula ( )
21
+ self . content = " "
30
22
}
31
23
}
32
24
@@ -39,39 +31,39 @@ public struct IF: GlobalElement {
39
31
public init ( _ condition: Conditionable , @ContentBuilder < AnyContent > content: ( ) -> AnyContent ) {
40
32
41
33
let condition = Condition ( condition: condition)
42
- condition. view = content ( )
34
+ condition. content = content ( )
43
35
44
36
self . conditions = [ condition]
45
37
}
46
38
47
39
public func elseIf( _ condition: Conditionable , @ContentBuilder < AnyContent > content: ( ) -> AnyContent ) -> IF {
48
40
49
41
let condition = Condition ( condition: condition)
50
- condition. view = content ( )
42
+ condition. content = content ( )
51
43
52
44
return . init( conditions: conditions + [ condition] )
53
45
}
54
46
55
47
public func elseIf< T> ( isNil path: TemplateValue < T ? > , @ContentBuilder < AnyContent > content: ( ) -> AnyContent ) -> IF {
56
48
57
49
let condition = Condition ( condition: IsNullCondition < T > ( lhs: path) )
58
- condition. view = content ( )
50
+ condition. content = content ( )
59
51
60
52
return . init( conditions: conditions + [ condition] )
61
53
}
62
54
63
55
public func elseIf< T> ( isNotNil path: TemplateValue < T ? > , @ContentBuilder < AnyContent > content: ( ) -> AnyContent ) -> IF {
64
56
65
57
let condition = Condition ( condition: NotNullCondition < T > ( lhs: path) )
66
- condition. view = content ( )
58
+ condition. content = content ( )
67
59
68
60
return . init( conditions: conditions + [ condition] )
69
61
}
70
62
71
63
public func `else`( @ContentBuilder < AnyContent > content: ( ) -> AnyContent ) -> IF {
72
64
73
65
let condition = Condition ( condition: AlwaysTrueCondition ( ) )
74
- condition. view = content ( )
66
+ condition. content = content ( )
75
67
76
68
return . init( conditions: conditions + [ condition] )
77
69
}
@@ -84,7 +76,7 @@ extension IF: AnyContent {
84
76
IF ( conditions: conditions. map { htmlCondition in
85
77
86
78
let scriptCondition = IF . Condition ( condition: htmlCondition. condition)
87
- scriptCondition. view = htmlCondition. view . scripts
79
+ scriptCondition. content = htmlCondition. content . scripts
88
80
89
81
return scriptCondition
90
82
} )
@@ -95,31 +87,22 @@ extension IF: AnyContent {
95
87
var isStaticallyEvaluated = true
96
88
97
89
for condition in conditions {
98
-
99
- condition. formula. calendar = formula. calendar
100
-
101
- condition. formula. timeZone = formula. timeZone
102
90
103
91
do {
104
92
105
93
guard isStaticallyEvaluated else {
106
- throw IFPrerenderErrors . dynamiclyEvaluatedCondition
94
+ break
107
95
}
108
96
109
- let testContext = ContextManager < Void > ( contexts: [ : ] )
110
-
111
- if try condition. condition. evaluate ( with: testContext) {
97
+ if try condition. condition. evaluate ( with: ContextManager < Void > ( contexts: [ : ] ) ) {
112
98
113
- try condition. view . prerender ( formula)
99
+ try condition. content . prerender ( formula)
114
100
115
- return // Returning as the first true condition should be the only one that is rendered
101
+ break
116
102
}
117
103
118
104
} catch {
119
-
120
105
isStaticallyEvaluated = false
121
-
122
- try condition. prerender ( formula)
123
106
}
124
107
}
125
108
@@ -144,11 +127,11 @@ extension IF: AnyContent {
144
127
extension IF . Condition : AnyContent {
145
128
146
129
public func prerender( _ formula: Formula ) throws {
147
- try view . prerender ( formula)
130
+ try content . prerender ( formula)
148
131
}
149
132
150
133
public func render< T> ( with manager: ContextManager < T > ) throws -> String {
151
- return try formula . render ( with: manager)
134
+ return try content . render ( with: manager)
152
135
}
153
136
}
154
137
0 commit comments