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

For tag does not support condition #45

Closed
tpetry opened this issue Oct 30, 2012 · 6 comments
Closed

For tag does not support condition #45

tpetry opened this issue Oct 30, 2012 · 6 comments

Comments

@tpetry
Copy link

tpetry commented Oct 30, 2012

Hi,
me again. You've done great work but i've found another bug. The twig manual describes how to use a condition inside a for loop. The implementation notes page for twig.js states that the for tag is "supported" but it does not support conditions inside a for tag.

Testcode:

var template = '{% for value in values if value == "a" %}{{value}}{% endfor %}';
Twig.twig({data: template}).render({values: ['a', 'a', 'b', 'b']});

Expected output:

aa

Real behaviour:

TwigException: Twig.expression.type.variable cannot follow a Twig.expression.type.variable at template:6 near 'if...' { message="Twig.expression.type.va...template:6 near 'if...'", name="TwigException", type="TwigException"}
@tpetry
Copy link
Author

tpetry commented Nov 2, 2012

thanks, that you fixed it so fast. But it has some minor flaw:

{% set outerloop = [1, 1, 1, 1] %}

{% set value = 'b' %}
{% set known_values = [{val: 'a'}, {val: 'b'}, {val: 'c'}] %}

{% for _ in outerloop %}
    {% for known in known_values if known.val == value %}
        match!
    {% else %}
        no match!
    {% endfor %}
{% endfor %}

The template has to output 4 times the "match!" string but does throw an error

@tpetry tpetry reopened this Nov 2, 2012
@justjohn
Copy link
Collaborator

justjohn commented Nov 2, 2012

I'm not surprised there's a bug in the implementation, what I committed is not well tested yet. I'll be working on it more this evening.

@tpetry
Copy link
Author

tpetry commented Nov 2, 2012

Ah okay, say when you're done and i try the implementation

@justjohn
Copy link
Collaborator

justjohn commented Nov 2, 2012

Also, you need to quote keys in object literals: (unquoted keys are new as of twig 1.5 which released after I wrote the object literal parser in twig.js, so they're not supported yet)

{% set known_values = [{'val': 'a'}, {'val': 'b'}, {'val': 'c'}] %}

justjohn added a commit that referenced this issue Nov 4, 2012
@justjohn
Copy link
Collaborator

justjohn commented Nov 4, 2012

It looks like the for loop was doing the correct thing, it was the unquoted object keys that were causing the error. I've added support for them, so your example should work now.

@justjohn
Copy link
Collaborator

This is fixed and available in 0.4.6.

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

2 participants