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

accessing an array element with a colon #854

Closed
tacman opened this issue Jan 13, 2023 · 3 comments · Fixed by #879
Closed

accessing an array element with a colon #854

tacman opened this issue Jan 13, 2023 · 3 comments · Fixed by #879

Comments

@tacman
Copy link

tacman commented Jan 13, 2023

I'm passing in a hydra json response, which includes an element with a key of 'hydra:member'.

My twig template is

<h3>
    {{ data['@type'] }}
</h3>
{% for d in data['hydra:member'] %}
{{ d.id }}
{% endfor %}

This throws an error

TwigException: Twig.expression.type.array.start cannot follow a Twig.expression.type.variable at template:4 near '[...'
ERROR Failed to compile with 1 errors Syntax Error: Unexpected token u in JSON at position 0
at JSON.parse ()

My solution is to set a temporary variable and use it, but I'm wondering if there's a better way, or if this can be fixed.

{% set hack = 'hydra:member' %}
{% for d in data[hack] %}
{{ d.id }}
{% endfor %}

Thanks. I think this library rocks.

@willrowe
Copy link
Collaborator

Can you provide an example of it working as expected in Twigfiddle?

@antoineveldhoven
Copy link
Contributor

antoineveldhoven commented Oct 22, 2023

Examples:

TwigPHP example
twig.js example

@antoineveldhoven
Copy link
Contributor

antoineveldhoven commented Oct 22, 2023

Indeed, I'm having a hard time reproducing it, or even finding where I used it in my code. Thanks, if I find it I'll reopen.

Please reopen this issue, since I've only provided the examples.
I've submitted a PR that could fix this issue, although I'm not sure if it will break anything else.

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

Successfully merging a pull request may close this issue.

3 participants