Skip to content

Commit 4468c7d

Browse files
committed
feat: Add a new DictRenderingContext protocol, for use with renderers that expect to use a dictionary instead of a struct
1 parent c0db842 commit 4468c7d

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Sources/Saga/RenderingContexts.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,73 @@ public struct PageRenderingContext: Sendable {
142142
public let translations: [SagaLocale: String]
143143
}
144144

145+
// MARK: - Dictionary conversion for template renderers such as Stencil
146+
147+
public protocol DictRenderingContext {
148+
func asDictionary() -> [String: Any]
149+
}
150+
151+
extension ItemRenderingContext: DictRenderingContext {
152+
public func asDictionary() -> [String: Any] {
153+
var dict: [String: Any] = [
154+
"item": item,
155+
"items": items,
156+
"allItems": allItems,
157+
"resources": resources,
158+
"translations": translations,
159+
]
160+
if let previous { dict["previous"] = previous }
161+
if let next { dict["next"] = next }
162+
if let subfolder { dict["subfolder"] = subfolder }
163+
if let locale { dict["locale"] = locale }
164+
return dict
165+
}
166+
}
167+
168+
extension ItemsRenderingContext: DictRenderingContext {
169+
public func asDictionary() -> [String: Any] {
170+
var dict: [String: Any] = [
171+
"items": items,
172+
"allItems": allItems,
173+
"outputPath": outputPath,
174+
"translations": translations,
175+
]
176+
if let paginator { dict["paginator"] = paginator }
177+
if let subfolder { dict["subfolder"] = subfolder }
178+
if let locale { dict["locale"] = locale }
179+
return dict
180+
}
181+
}
182+
183+
extension PartitionedRenderingContext: DictRenderingContext {
184+
public func asDictionary() -> [String: Any] {
185+
var dict: [String: Any] = [
186+
"key": key,
187+
"items": items,
188+
"allItems": allItems,
189+
"outputPath": outputPath,
190+
"translations": translations,
191+
]
192+
if let paginator { dict["paginator"] = paginator }
193+
if let subfolder { dict["subfolder"] = subfolder }
194+
if let locale { dict["locale"] = locale }
195+
return dict
196+
}
197+
}
198+
199+
extension PageRenderingContext: DictRenderingContext {
200+
public func asDictionary() -> [String: Any] {
201+
var dict: [String: Any] = [
202+
"allItems": allItems,
203+
"outputPath": outputPath,
204+
"generatedPages": generatedPages,
205+
"translations": translations,
206+
]
207+
if let locale { dict["locale"] = locale }
208+
return dict
209+
}
210+
}
211+
145212
/// A model representing a paginator.
146213
///
147214
/// When you use the `listWriter` or one of the `partitionedWriter` versions, you can choose to paginate the items into multiple output files.

0 commit comments

Comments
 (0)