Moving stuff about, renaming
This commit is contained in:
@@ -8,10 +8,14 @@ public class HBMustacheLibrary {
|
|||||||
templates[name] = template
|
templates[name] = template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func getTemplate(named name: String) -> HBMustacheTemplate? {
|
||||||
|
templates[name]
|
||||||
|
}
|
||||||
|
|
||||||
public func render(_ object: Any, withTemplateNamed name: String) -> String? {
|
public func render(_ object: Any, withTemplateNamed name: String) -> String? {
|
||||||
guard let template = templates[name] else { return nil }
|
guard let template = templates[name] else { return nil }
|
||||||
return template.render(object, library: self)
|
return template.render(object, library: self)
|
||||||
}
|
}
|
||||||
|
|
||||||
var templates: [String: HBMustacheTemplate]
|
private var templates: [String: HBMustacheTemplate]
|
||||||
}
|
}
|
||||||
|
|||||||
17
Sources/HummingbirdMustache/Mirror.swift
Normal file
17
Sources/HummingbirdMustache/Mirror.swift
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
|
||||||
|
func unwrapOptional(_ object: Any) -> Any? {
|
||||||
|
let mirror = Mirror(reflecting: object)
|
||||||
|
guard mirror.displayStyle == .optional else { return object }
|
||||||
|
guard let first = mirror.children.first else { return nil }
|
||||||
|
return first.value
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Mirror {
|
||||||
|
func getValue(forKey key: String) -> Any? {
|
||||||
|
guard let matched = children.filter({ $0.label == key }).first else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return unwrapOptional(matched.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
|
|
||||||
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, library: HBMustacheLibrary?) -> String
|
|
||||||
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Array: HBSequence {
|
|
||||||
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
|
|
||||||
var string = ""
|
|
||||||
for obj in self {
|
|
||||||
string += template.render(obj, library: library)
|
|
||||||
}
|
|
||||||
return string
|
|
||||||
}
|
|
||||||
|
|
||||||
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
|
|
||||||
if count == 0 {
|
|
||||||
return template.render(self, library: library)
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -69,7 +69,7 @@ extension HBMustacheTemplate {
|
|||||||
childObject = customBox.child(named: name)
|
childObject = customBox.child(named: name)
|
||||||
} else {
|
} else {
|
||||||
let mirror = Mirror(reflecting: object)
|
let mirror = Mirror(reflecting: object)
|
||||||
childObject = mirror.getAttribute(forKey: name)
|
childObject = mirror.getValue(forKey: name)
|
||||||
}
|
}
|
||||||
guard childObject != nil else { return nil }
|
guard childObject != nil else { return nil }
|
||||||
let names2 = names.dropFirst()
|
let names2 = names.dropFirst()
|
||||||
@@ -100,19 +100,37 @@ extension HBMustacheTemplate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func unwrap(_ any: Any) -> Any? {
|
protocol HBMustacheParent {
|
||||||
let mirror = Mirror(reflecting: any)
|
func child(named: String) -> Any?
|
||||||
guard mirror.displayStyle == .optional else { return any }
|
|
||||||
guard let first = mirror.children.first else { return nil }
|
|
||||||
return first.value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Mirror {
|
extension HBMustacheParent {
|
||||||
func getAttribute(forKey key: String) -> Any? {
|
// default child to nil
|
||||||
guard let matched = children.filter({ $0.label == key }).first else {
|
func child(named: String) -> Any? { return nil }
|
||||||
return nil
|
}
|
||||||
|
|
||||||
|
extension Dictionary: HBMustacheParent where Key == String {
|
||||||
|
func child(named: String) -> Any? { return self[named] }
|
||||||
|
}
|
||||||
|
|
||||||
|
protocol HBSequence {
|
||||||
|
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String
|
||||||
|
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Array: HBSequence {
|
||||||
|
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
|
||||||
|
var string = ""
|
||||||
|
for obj in self {
|
||||||
|
string += template.render(obj, library: library)
|
||||||
}
|
}
|
||||||
return unwrap(matched.value)
|
return string
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
|
||||||
|
if count == 0 {
|
||||||
|
return template.render(self, library: library)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -110,17 +110,17 @@ final class TemplateRendererTests: XCTestCase {
|
|||||||
|
|
||||||
func testMustacheManualExample2() throws {
|
func testMustacheManualExample2() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
* {{name}}
|
*{{name}}
|
||||||
* {{age}}
|
*{{age}}
|
||||||
* {{company}}
|
*{{company}}
|
||||||
* {{{company}}}
|
*{{{company}}}
|
||||||
""")
|
""")
|
||||||
let object: [String: Any] = ["name": "Chris", "company": "<b>GitHub</b>"]
|
let object: [String: Any] = ["name": "Chris", "company": "<b>GitHub</b>"]
|
||||||
XCTAssertEqual(template.render(object), """
|
XCTAssertEqual(template.render(object), """
|
||||||
* Chris
|
*Chris
|
||||||
*
|
*
|
||||||
* <b>GitHub</b>
|
*<b>GitHub</b>
|
||||||
* <b>GitHub</b>
|
*<b>GitHub</b>
|
||||||
""")
|
""")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user