diff --git a/Sources/HummingbirdMustache/Library+FileSystem.swift b/Sources/HummingbirdMustache/Library+FileSystem.swift new file mode 100644 index 0000000..bab2fc0 --- /dev/null +++ b/Sources/HummingbirdMustache/Library+FileSystem.swift @@ -0,0 +1,23 @@ +import Foundation + +extension HBMustacheLibrary { + func loadTemplates(from directory: String, withExtension extension: String = "mustache") { + var directory = directory + if !directory.hasSuffix("/") { + directory += "/" + } + let extWithDot = ".\(`extension`)" + let fs = FileManager() + guard let enumerator = fs.enumerator(atPath: directory) else { return } + for case let path as String in enumerator { + guard path.hasSuffix(extWithDot) else { continue } + guard let data = fs.contents(atPath: directory + path) else { continue} + let string = String(decoding: data, as: Unicode.UTF8.self) + guard let template = try? HBMustacheTemplate(string: string) else { continue } + + // drop ".mustache" from path to get name + let name = String(path.dropLast(extWithDot.count)) + register(template, named: name) + } + } +} diff --git a/Sources/HummingbirdMustache/Library.swift b/Sources/HummingbirdMustache/Library.swift index f3e5d21..6f4970c 100644 --- a/Sources/HummingbirdMustache/Library.swift +++ b/Sources/HummingbirdMustache/Library.swift @@ -1,9 +1,14 @@ public class HBMustacheLibrary { - init() { + public init() { self.templates = [:] } + public init(directory: String) { + self.templates = [:] + self.loadTemplates(from: directory) + } + public func register(_ template: HBMustacheTemplate, named name: String) { templates[name] = template } diff --git a/Tests/HummingbirdMustacheTests/LibraryTests.swift b/Tests/HummingbirdMustacheTests/LibraryTests.swift new file mode 100644 index 0000000..efa30d4 --- /dev/null +++ b/Tests/HummingbirdMustacheTests/LibraryTests.swift @@ -0,0 +1,18 @@ +import XCTest +@testable import HummingbirdMustache + +final class LibraryTests: XCTestCase { + func testDirectoryLoad() throws { + let fs = FileManager() + try fs.createDirectory(atPath: "./templates", withIntermediateDirectories: false) + let mustache = "{{#value}}{{.}}{{/value}}" + let data = Data(mustache.utf8) + defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) } + try data.write(to: URL(fileURLWithPath: "templates/test.mustache")) + defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test.mustache")) } + + let library = HBMustacheLibrary(directory: "./templates") + let object = ["value": ["value1", "value2"]] + XCTAssertEqual(library.render(object, withTemplateNamed: "test"), "value1value2") + } +} diff --git a/Tests/HummingbirdMustacheTests/PartialTests.swift b/Tests/HummingbirdMustacheTests/PartialTests.swift new file mode 100644 index 0000000..06ae770 --- /dev/null +++ b/Tests/HummingbirdMustacheTests/PartialTests.swift @@ -0,0 +1,54 @@ +import XCTest +@testable import HummingbirdMustache + +final class PartialTests: XCTestCase { + + /// Testing partials + func testMustacheManualExample9() throws { + let library = HBMustacheLibrary() + let template = try HBMustacheTemplate(string: """ +

Names

+ {{#names}} + {{> user}} + {{/names}} + """) + let template2 = try HBMustacheTemplate(string: """ + {{.}} + """) + library.register(template, named: "base") + library.register(template2, named: "user") + + let object: [String: Any] = ["names": ["john", "adam", "claire"]] + XCTAssertEqual(library.render(object, withTemplateNamed: "base"), """ +

Names

+ john + adam + claire + + """) + } + + /// Testing dynamic partials + func testDynamicPartials() throws { + let library = HBMustacheLibrary() + let template = try HBMustacheTemplate(string: """ +

Names

+ {{partial}} + """) + let template2 = try HBMustacheTemplate(string: """ + {{#names}} + {{.}} + {{/names}} + """) + library.register(template, named: "base") + + let object: [String: Any] = ["names": ["john", "adam", "claire"], "partial": template2] + XCTAssertEqual(library.render(object, withTemplateNamed: "base"), """ +

Names

+ john + adam + claire + + """) + } +} diff --git a/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift b/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift index 0205f14..f3af40a 100644 --- a/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift +++ b/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift @@ -191,53 +191,4 @@ final class TemplateRendererTests: XCTestCase {

Today.

""") } - - /// Testing partials - func testMustacheManualExample9() throws { - let library = HBMustacheLibrary() - let template = try HBMustacheTemplate(string: """ -

Names

- {{#names}} - {{> user}} - {{/names}} - """) - let template2 = try HBMustacheTemplate(string: """ - {{.}} - """) - library.register(template, named: "base") - library.register(template2, named: "user") - - let object: [String: Any] = ["names": ["john", "adam", "claire"]] - XCTAssertEqual(library.render(object, withTemplateNamed: "base"), """ -

Names

- john - adam - claire - - """) - } - - /// Testing dynamic partials - func testDynamicPartials() throws { - let library = HBMustacheLibrary() - let template = try HBMustacheTemplate(string: """ -

Names

- {{partial}} - """) - let template2 = try HBMustacheTemplate(string: """ - {{#names}} - {{.}} - {{/names}} - """) - library.register(template, named: "base") - - let object: [String: Any] = ["names": ["john", "adam", "claire"], "partial": template2] - XCTAssertEqual(library.render(object, withTemplateNamed: "base"), """ -

Names

- john - adam - claire - - """) - } }