stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.34k stars 223 forks source link

`default` filter doesn't seem to work on dictionary elements #161

Closed NocturnalSolutions closed 6 years ago

NocturnalSolutions commented 6 years ago
import Stencil

let aNilString: String? = nil

var testContext: [String: Any] = [
    "notNil": "I'm Not Nil" as Any,
    "nil": aNilString as Any
]

testContext["subDictionary"] = testContext

let templateString = """
{{ notNil|default:"It's Nil" }}
{{ nil|default:"It's Nil" }}
{{ subDictionary.notNil|default:"It's Nil" }}
{{ subDictionary.nil|default:"It's Nil" }}
"""

let template = Template(templateString: templateString)
let result = try template.render(testContext)
print(result)

Expected result:

I'm Not Nil
It's Nil
I'm Not Nil
It's Nil

Actual result:

I'm Not Nil
It's Nil
I'm Not Nil

Here's a full project you can clone and run to see this happening in real time. This is a distillation of behavior I'm seeing happening on a "real" project (a Kitura site).

Other filters such as uppercase seem to work fine on dictionary elements, though I haven't tested all of them.