File tree 4 files changed +59
-4
lines changed
Sources/HummingbirdMustache
Tests/HummingbirdMustacheTests
4 files changed +59
-4
lines changed Original file line number Diff line number Diff line change @@ -27,14 +27,16 @@ extension HBMustacheTemplate {
27
27
28
28
if let indentation = context. indentation, indentation != " " {
29
29
for token in tokens {
30
- if string. last == " \n " {
30
+ let renderedString = self . renderToken ( token, context: & context)
31
+ if renderedString != " " , string. last == " \n " {
31
32
string += indentation
32
33
}
33
- string += self . renderToken ( token , context : & context )
34
+ string += renderedString
34
35
}
35
36
} else {
36
37
for token in tokens {
37
- string += self . renderToken ( token, context: & context)
38
+ let result = self . renderToken ( token, context: & context)
39
+ string += result
38
40
}
39
41
}
40
42
return string
Original file line number Diff line number Diff line change @@ -39,6 +39,8 @@ public extension StringProtocol {
39
39
/// - Returns: Result
40
40
func transform( _ name: String ) -> Any ? {
41
41
switch name {
42
+ case " empty " :
43
+ return isEmpty
42
44
case " capitalized " :
43
45
return capitalized
44
46
case " lowercased " :
Original file line number Diff line number Diff line change @@ -42,6 +42,41 @@ final class PartialTests: XCTestCase {
42
42
""" )
43
43
}
44
44
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
+
45
80
/// Testing dynamic partials
46
81
func testDynamicPartials( ) throws {
47
82
let library = HBMustacheLibrary ( )
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ final class TemplateRendererTests: XCTestCase {
82
82
XCTAssertEqual ( template. render ( Test ( string: nil ) ) , " test " )
83
83
}
84
84
85
- func testOptionalSequence ( ) throws {
85
+ func testOptionalSection ( ) throws {
86
86
struct Test {
87
87
let string : String ?
88
88
}
@@ -94,6 +94,22 @@ final class TemplateRendererTests: XCTestCase {
94
94
XCTAssertEqual ( template2. render ( Test ( string: nil ) ) , " test * " )
95
95
}
96
96
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
+
97
113
func testStructureInStructure( ) throws {
98
114
struct SubTest {
99
115
let string : String ?
You can’t perform that action at this time.
0 commit comments