Allow conditions in variable node (#243)

* use condition in variable node

* added support for else expression

* addressing code review comments
This commit is contained in:
Ilya Puchka
2018-09-22 14:09:25 +03:00
committed by GitHub
parent 2c3962a3de
commit df2e193891
4 changed files with 69 additions and 3 deletions

View File

@@ -337,4 +337,23 @@ func testVariable() {
}
}
describe("inline if expression") {
$0.it("can conditionally render variable") {
let template: Template = "{{ variable if variable|uppercase == \"A\" }}"
try expect(template.render(Context(dictionary: ["variable": "a"]))) == "a"
try expect(template.render(Context(dictionary: ["variable": "b"]))) == ""
}
$0.it("can render with else expression") {
let template: Template = "{{ variable if variable|uppercase == \"A\" else fallback|uppercase }}"
try expect(template.render(Context(dictionary: ["variable": "b", "fallback": "c"]))) == "C"
}
$0.it("throws when used invalid condition") {
let template: Template = "{{ variable if variable \"A\" }}"
try expect(template.render(Context(dictionary: ["variable": "a"]))).toThrow()
}
}
}