user3531149
user3531149

Reputation: 1539

How to use if else for a controller variable on twig

What im trying to do is compare the imageowner (variable returned by the controller) with the current user

<!-- image remove button --> 
 {% if {{ imageOwner }} == {{ app.user.username }} %} //Line 5
    <p>You are the owner of this image</p>

    <button onclick="deleteStuff()">deleted</button>
 {% endif %}

but it throws this error

A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{" inLayoutBundle:Frontpage:content-bubble.html.twig at line 5

the {{ imageOwner }} and {{ app.user.username }} return the right values, I think its a syntax or logical error

Upvotes: 0

Views: 769

Answers (1)

xiidea
xiidea

Reputation: 3394

Use

 {% if imageOwner  == app.user.username %} //Line 5
    <p>You are the owner of this image</p>

    <button onclick="deleteStuff()">deleted</button>
 {% endif %}

{{ imageOwner }} is slimier to echo $imageOwner in php. So you do not need to wrap variable with {{}} when you don't want to print it.

Upvotes: 3

Related Questions