Skip to content

Commit

Permalink
Re-instate register functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Jan 30, 2024
1 parent c9d5021 commit c2c77d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
8 changes: 8 additions & 0 deletions Sources/HummingbirdMustache/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public struct HBMustacheLibrary: Sendable {
self.templates = try Self.loadTemplates(from: directory, withExtension: `extension`)
}

/// Register template under name
/// - Parameters:
/// - template: Template
/// - name: Name of template
public mutating func register(_ template: HBMustacheTemplate, named name: String) {
self.templates[name] = template
}

/// Register template under name
/// - Parameters:
/// - mustache: Mustache text
Expand Down
35 changes: 6 additions & 29 deletions Tests/HummingbirdMustacheTests/PartialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ final class PartialTests: XCTestCase {
{{/empty(.)}}
""")
let library = HBMustacheLibrary(templates: ["base": template, "user": template2])
var library = HBMustacheLibrary()
library.register(template, named: "base")
library.register(template2, named: "user") // , withTemplate: String)// = HBMustacheLibrary(templates: ["base": template, "user": template2])

let object: [String: Any] = ["names": ["john", "adam", "claire"]]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
Expand Down Expand Up @@ -98,33 +100,8 @@ final class PartialTests: XCTestCase {

/// test inheritance
func testInheritance() throws {
let library = try HBMustacheLibrary(templates: [
"header": .init(string: """
<head>
<title>{{$title}}Default title{{/title}}</title>
</head>
"""),
"base": .init(string: """
<html>
{{$header}}{{/header}}
{{$content}}{{/content}}
</html>
"""),
"mypage": .init(string: """
{{<base}}
{{$header}}
{{<header}}
{{$title}}My page title{{/title}}
{{/header}}
{{/header}}
{{$content}}<h1>Hello world</h1>{{/content}}
{{/base}}
""")
])
/* try library.register(
var library = HBMustacheLibrary()
try library.register(
"""
<head>
<title>{{$title}}Default title{{/title}}</title>
Expand Down Expand Up @@ -156,7 +133,7 @@ final class PartialTests: XCTestCase {
""",
named: "mypage"
)*/
)
XCTAssertEqual(library.render({}, withTemplate: "mypage")!, """
<html>
<head>
Expand Down

0 comments on commit c2c77d2

Please sign in to comment.