Open svanimpe opened 5 years ago
Is that date
key from your parent context containing a string or a full dictionary? If it's not a dictionary that might explain it?
It should be a dictionary. It's generated by Kitura from the following Codable struct:
struct DateViewModel: Codable {
let day: Int
let month: Int
let year: Int
let hour: Int
let minute: Int
init(_ components: DateComponents) {
day = components.day!
month = components.month!
year = components.year!
hour = components.hour!
minute = components.minute!
}
}
which is used as a property in my parent context:
let date: DateViewModel
Mmmh strange indeed. Haven't re-tested that recently, but I remember it working (outside of Kitura though, but shouldn't really change much) a while ago…
@ilyapuchka any idea?
The behaviour won't support embedding codable object like you have. The include sub-context must be a dictionary itself.
Hmm. I've checked what Kitura does, and it's basically just generating a JSON dictionary from my Codable:
let data = try JSONEncoder().encode(value)
let dict = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
(somewhat simplified)
In this case, my date
variable is probably Any
, but it should be castable to [String: Int]
.
Shouldn't this work?
@kylef Do you know if anyone actually uses this feature? I'd love to see some examples of it, so I can figure out what works and what doesn't.
@svanimpe There is a test-case at https://github.com/stencilproject/Stencil/blob/fc404b25d81f93ca48aa48a6fc50a9c0dce88128/Tests/StencilTests/IncludeSpec.swift#L61 perhaps you can adapt that to be closer to what you are trying to achieve to debug.
I've tried changing that test to use Codable, as follows:
$0.it("successfully passes context") {
struct TestChild: Codable {
let target: String
}
struct TestParent: Codable {
let child: TestChild
}
let parent = TestParent(child: TestChild(target: "World"))
let data = try JSONEncoder().encode(parent)
let dict = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
let template = Template(templateString: "{% include \"test.html\" child %}")
let context = Context(dictionary: dict, environment: environment)
let value = try template.render(context)
try expect(value) == "Hello World!"
}
But that still passes, so the issue must be elsewhere 🤔
From https://stencil.fuller.li/en/latest/builtins.html#built-in-tags:
That's not working for me. I'm getting a "unable to render context" error.
This is my code:
{% include "snippets/date-time-picker.stencil" date %}
date
is included in my context (I'm using it in other places as well). Since that snippet only usesdate
, I was hoping to remove thedate.
prefix from all my variables.