Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include with subcontext not working #223

Open
svanimpe opened this issue Aug 6, 2018 · 8 comments
Open

Include with subcontext not working #223

svanimpe opened this issue Aug 6, 2018 · 8 comments

Comments

@svanimpe
Copy link
Member

svanimpe commented Aug 6, 2018

From https://stencil.fuller.li/en/latest/builtins.html#built-in-tags:

By default the included file gets passed the current context. You can pass a sub context by using an optional 2nd parameter as a lookup in the current context.

{% include "comment.html" comment %}

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 uses date, I was hoping to remove the date. prefix from all my variables.

@AliSoftware
Copy link
Collaborator

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?

@svanimpe
Copy link
Member Author

svanimpe commented Aug 7, 2018

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

@AliSoftware
Copy link
Collaborator

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?

@kylef
Copy link
Collaborator

kylef commented Aug 9, 2018

The behaviour won't support embedding codable object like you have. The include sub-context must be a dictionary itself.

@svanimpe
Copy link
Member Author

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?

@svanimpe
Copy link
Member Author

@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.

@kylef
Copy link
Collaborator

kylef commented Aug 29, 2018

@svanimpe There is a test-case at

let template = Template(templateString: "{% include \"test.html\" child %}")
perhaps you can adapt that to be closer to what you are trying to achieve to debug.

@svanimpe
Copy link
Member Author

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 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants