stencilproject / Stencil

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

Handle double optionals #110

Closed tomcomer closed 7 years ago

tomcomer commented 7 years ago

When using Stencil with Vapor, I found that the id field of model objects would resolve to a double optional within Stencil. The id field is defined as Node? and the current variable here is defined as Any?, so what it ends up being is Optional(Optional(Node)). The code I added below handles that case.

kylef commented 7 years ago

Hi @tomcomer, thanks for your contribution. Would you be able to add a test-case to demonstrate this behaviour and to prevent any further regressions in the future.

tomcomer commented 7 years ago

I've added a test-case and updated the PR.

kylef commented 7 years ago

This test passes without other sources changes. Either this is already fixed, this isn't a real bug, or this test isn't testing the desired behaviour:

$ hub fetch tomcomer
$ git checkout tomcomer/master Tests/StencilTests/VariableSpec.swift
$ swift test

I'm going to close, please feel free to create a new pull request if you can demonstrate there is a bug with a test that fails on master.

djbe commented 6 years ago

@kylef This issue is still present for me. Say I have the following data:

final class Entity {
    let name: String
    weak var parent: Entity?
}

let context = [
  "entities": [... a bunch of entities here...]
]

And I try the following template:

{% for entity in entities %}
  {{ entity.name }}: {{ entity.parent.name }}
{% endfor %}

It will never reach the name property of a parent, because:

Funnily enough, the following template does work:

{% for entity in entities %}
  {{ entity.name }}: {{ entity.parent.some.name }}
{% endfor %}

But that's not very intuitive for users.

ilyapuchka commented 6 years ago

@djbe I think #204 should fix this