Skip to content

Commit 483e364

Browse files
authored
Merge pull request #36 from bygri/if-blank-string
Proposal: #if(string) to not render if string is blank
2 parents 7d0433b + b7c98bb commit 483e364

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#if(name) { Hello, there! }

Sources/Leaf/Tag/Models/If.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ public final class If: Tag {
2121
arguments: [Argument],
2222
value: Node?) -> Bool {
2323
guard let value = arguments.first?.value else { return false }
24-
// Existence of bool, evaluate bool. Otherwise, not-nil returns true.
25-
guard let bool = value.bool else { return true }
26-
return bool
24+
// Existence of bool, evaluate bool.
25+
if let bool = value.bool { return bool }
26+
// Empty string value returns false.
27+
if value.string == "" { return false }
28+
// Otherwise, not-nil returns true.
29+
return true
2730
}
2831
}

Tests/LeafTests/IfTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class IfTests: XCTestCase {
99
("testBasicIfElse", testBasicIfElse),
1010
("testNestedIfElse", testNestedIfElse),
1111
("testIfThrow", testIfThrow),
12+
("testIfEmptyString", testIfEmptyString),
1213
]
1314

1415
func testBasicIf() throws {
@@ -78,4 +79,22 @@ class IfTests: XCTestCase {
7879
XCTFail("should throw")
7980
} catch If.Error.expectedSingleArgument {}
8081
}
82+
83+
func testIfEmptyString() throws {
84+
let template = try stem.spawnLeaf(named: "if-empty-string-test")
85+
do {
86+
let context = try Node(node: ["name": "name"])
87+
let loadable = Context(context)
88+
let rendered = try stem.render(template, with: loadable).string
89+
let expectation = "Hello, there!"
90+
XCTAssert(rendered == expectation, "have: \(rendered), want: \(expectation)")
91+
}
92+
do {
93+
let context = try Node(node: ["name": ""])
94+
let loadable = Context(context)
95+
let rendered = try stem.render(template, with: loadable).string
96+
let expectation = ""
97+
XCTAssert(rendered == expectation, "have: \(rendered), want: \(expectation)")
98+
}
99+
}
81100
}

0 commit comments

Comments
 (0)