Skip to content

Commit fa56176

Browse files
authored
Fix issue with indented partials (#19)
* Fix issue with indented partials * Add test * Add empty transform for string
1 parent 185004f commit fa56176

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

Sources/HummingbirdMustache/Template+Render.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ extension HBMustacheTemplate {
2727

2828
if let indentation = context.indentation, indentation != "" {
2929
for token in tokens {
30-
if string.last == "\n" {
30+
let renderedString = self.renderToken(token, context: &context)
31+
if renderedString != "", string.last == "\n" {
3132
string += indentation
3233
}
33-
string += self.renderToken(token, context: &context)
34+
string += renderedString
3435
}
3536
} else {
3637
for token in tokens {
37-
string += self.renderToken(token, context: &context)
38+
let result = self.renderToken(token, context: &context)
39+
string += result
3840
}
3941
}
4042
return string

Sources/HummingbirdMustache/Transform.swift

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public extension StringProtocol {
3939
/// - Returns: Result
4040
func transform(_ name: String) -> Any? {
4141
switch name {
42+
case "empty":
43+
return isEmpty
4244
case "capitalized":
4345
return capitalized
4446
case "lowercased":

Tests/HummingbirdMustacheTests/PartialTests.swift

+35
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,41 @@ final class PartialTests: XCTestCase {
4242
""")
4343
}
4444

45+
/// Test where last line of partial generates no content. It should not add a
46+
/// tab either
47+
func testPartialEmptyLineTabbing() throws {
48+
let library = HBMustacheLibrary()
49+
let template = try HBMustacheTemplate(string: """
50+
<h2>Names</h2>
51+
{{#names}}
52+
{{> user}}
53+
{{/names}}
54+
Text after
55+
56+
""")
57+
let template2 = try HBMustacheTemplate(string: """
58+
{{^empty(.)}}
59+
<strong>{{.}}</strong>
60+
{{/empty(.)}}
61+
{{#empty(.)}}
62+
<strong>empty</strong>
63+
{{/empty(.)}}
64+
65+
""")
66+
library.register(template, named: "base")
67+
library.register(template2, named: "user")
68+
69+
let object: [String: Any] = ["names": ["john", "adam", "claire"]]
70+
XCTAssertEqual(library.render(object, withTemplate: "base"), """
71+
<h2>Names</h2>
72+
<strong>john</strong>
73+
<strong>adam</strong>
74+
<strong>claire</strong>
75+
Text after
76+
77+
""")
78+
}
79+
4580
/// Testing dynamic partials
4681
func testDynamicPartials() throws {
4782
let library = HBMustacheLibrary()

Tests/HummingbirdMustacheTests/TemplateRendererTests.swift

+17-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ final class TemplateRendererTests: XCTestCase {
8282
XCTAssertEqual(template.render(Test(string: nil)), "test ")
8383
}
8484

85-
func testOptionalSequence() throws {
85+
func testOptionalSection() throws {
8686
struct Test {
8787
let string: String?
8888
}
@@ -94,6 +94,22 @@ final class TemplateRendererTests: XCTestCase {
9494
XCTAssertEqual(template2.render(Test(string: nil)), "test *")
9595
}
9696

97+
func testOptionalSequence() throws {
98+
struct Test {
99+
let string: String?
100+
}
101+
let template = try HBMustacheTemplate(string: "test {{#.}}{{string}}{{/.}}")
102+
XCTAssertEqual(template.render([Test(string: "string")]), "test string")
103+
}
104+
105+
func testOptionalSequenceSection() throws {
106+
struct Test {
107+
let string: String?
108+
}
109+
let template = try HBMustacheTemplate(string: "test {{#.}}{{#string}}*{{.}}{{/string}}{{/.}}")
110+
XCTAssertEqual(template.render([Test(string: "string")]), "test *string")
111+
}
112+
97113
func testStructureInStructure() throws {
98114
struct SubTest {
99115
let string: String?

0 commit comments

Comments
 (0)