Add template library loading from FileSystem
This commit is contained in:
23
Sources/HummingbirdMustache/Library+FileSystem.swift
Normal file
23
Sources/HummingbirdMustache/Library+FileSystem.swift
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,14 @@
|
|||||||
|
|
||||||
public class HBMustacheLibrary {
|
public class HBMustacheLibrary {
|
||||||
init() {
|
public init() {
|
||||||
self.templates = [:]
|
self.templates = [:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public init(directory: String) {
|
||||||
|
self.templates = [:]
|
||||||
|
self.loadTemplates(from: directory)
|
||||||
|
}
|
||||||
|
|
||||||
public func register(_ template: HBMustacheTemplate, named name: String) {
|
public func register(_ template: HBMustacheTemplate, named name: String) {
|
||||||
templates[name] = template
|
templates[name] = template
|
||||||
}
|
}
|
||||||
|
|||||||
18
Tests/HummingbirdMustacheTests/LibraryTests.swift
Normal file
18
Tests/HummingbirdMustacheTests/LibraryTests.swift
Normal file
@@ -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 = "<test>{{#value}}<value>{{.}}</value>{{/value}}</test>"
|
||||||
|
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"), "<test><value>value1</value><value>value2</value></test>")
|
||||||
|
}
|
||||||
|
}
|
||||||
54
Tests/HummingbirdMustacheTests/PartialTests.swift
Normal file
54
Tests/HummingbirdMustacheTests/PartialTests.swift
Normal file
@@ -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: """
|
||||||
|
<h2>Names</h2>
|
||||||
|
{{#names}}
|
||||||
|
{{> user}}
|
||||||
|
{{/names}}
|
||||||
|
""")
|
||||||
|
let template2 = try HBMustacheTemplate(string: """
|
||||||
|
<strong>{{.}}</strong>
|
||||||
|
""")
|
||||||
|
library.register(template, named: "base")
|
||||||
|
library.register(template2, named: "user")
|
||||||
|
|
||||||
|
let object: [String: Any] = ["names": ["john", "adam", "claire"]]
|
||||||
|
XCTAssertEqual(library.render(object, withTemplateNamed: "base"), """
|
||||||
|
<h2>Names</h2>
|
||||||
|
<strong>john</strong>
|
||||||
|
<strong>adam</strong>
|
||||||
|
<strong>claire</strong>
|
||||||
|
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Testing dynamic partials
|
||||||
|
func testDynamicPartials() throws {
|
||||||
|
let library = HBMustacheLibrary()
|
||||||
|
let template = try HBMustacheTemplate(string: """
|
||||||
|
<h2>Names</h2>
|
||||||
|
{{partial}}
|
||||||
|
""")
|
||||||
|
let template2 = try HBMustacheTemplate(string: """
|
||||||
|
{{#names}}
|
||||||
|
<strong>{{.}}</strong>
|
||||||
|
{{/names}}
|
||||||
|
""")
|
||||||
|
library.register(template, named: "base")
|
||||||
|
|
||||||
|
let object: [String: Any] = ["names": ["john", "adam", "claire"], "partial": template2]
|
||||||
|
XCTAssertEqual(library.render(object, withTemplateNamed: "base"), """
|
||||||
|
<h2>Names</h2>
|
||||||
|
<strong>john</strong>
|
||||||
|
<strong>adam</strong>
|
||||||
|
<strong>claire</strong>
|
||||||
|
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -191,53 +191,4 @@ final class TemplateRendererTests: XCTestCase {
|
|||||||
<h1>Today.</h1>
|
<h1>Today.</h1>
|
||||||
""")
|
""")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Testing partials
|
|
||||||
func testMustacheManualExample9() throws {
|
|
||||||
let library = HBMustacheLibrary()
|
|
||||||
let template = try HBMustacheTemplate(string: """
|
|
||||||
<h2>Names</h2>
|
|
||||||
{{#names}}
|
|
||||||
{{> user}}
|
|
||||||
{{/names}}
|
|
||||||
""")
|
|
||||||
let template2 = try HBMustacheTemplate(string: """
|
|
||||||
<strong>{{.}}</strong>
|
|
||||||
""")
|
|
||||||
library.register(template, named: "base")
|
|
||||||
library.register(template2, named: "user")
|
|
||||||
|
|
||||||
let object: [String: Any] = ["names": ["john", "adam", "claire"]]
|
|
||||||
XCTAssertEqual(library.render(object, withTemplateNamed: "base"), """
|
|
||||||
<h2>Names</h2>
|
|
||||||
<strong>john</strong>
|
|
||||||
<strong>adam</strong>
|
|
||||||
<strong>claire</strong>
|
|
||||||
|
|
||||||
""")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Testing dynamic partials
|
|
||||||
func testDynamicPartials() throws {
|
|
||||||
let library = HBMustacheLibrary()
|
|
||||||
let template = try HBMustacheTemplate(string: """
|
|
||||||
<h2>Names</h2>
|
|
||||||
{{partial}}
|
|
||||||
""")
|
|
||||||
let template2 = try HBMustacheTemplate(string: """
|
|
||||||
{{#names}}
|
|
||||||
<strong>{{.}}</strong>
|
|
||||||
{{/names}}
|
|
||||||
""")
|
|
||||||
library.register(template, named: "base")
|
|
||||||
|
|
||||||
let object: [String: Any] = ["names": ["john", "adam", "claire"], "partial": template2]
|
|
||||||
XCTAssertEqual(library.render(object, withTemplateNamed: "base"), """
|
|
||||||
<h2>Names</h2>
|
|
||||||
<strong>john</strong>
|
|
||||||
<strong>adam</strong>
|
|
||||||
<strong>claire</strong>
|
|
||||||
|
|
||||||
""")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user