Files
swiftpm-mustache/Sources/HummingbirdMustache/Renderable.swift
Adam Fowler 7f61c8dd72 HBTemplate -> HBMustacheTemplate, escape characters
Add tests for mustache examples
2021-03-12 07:43:09 +00:00

53 lines
1.3 KiB
Swift

protocol HBMustacheParent {
func child(named: String) -> Any?
}
extension HBMustacheParent {
// default child to nil
func child(named: String) -> Any? { return nil }
}
extension Dictionary: HBMustacheParent where Key == String {
func child(named: String) -> Any? { return self[named] }
}
protocol HBSequence {
func renderSection(with template: HBMustacheTemplate) -> String
func renderInvertedSection(with template: HBMustacheTemplate) -> String
}
extension Array: HBSequence {
func renderSection(with template: HBMustacheTemplate) -> String {
var string = ""
for obj in self {
string += template.render(obj)
}
return string
}
func renderInvertedSection(with template: HBMustacheTemplate) -> String {
if count == 0 {
return template.render(self)
}
return ""
}
}
extension Dictionary: HBSequence {
func renderSection(with template: HBMustacheTemplate) -> String {
var string = ""
for obj in self {
string += template.render(obj)
}
return string
}
func renderInvertedSection(with template: HBMustacheTemplate) -> String {
if count == 0 {
return template.render(self)
}
return ""
}
}