We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Volt - If statement not working in for loop

Hi. I have somewhat stupid problem with, as a title says, an if statement in for loop. I'm trying to list all users in a <select>, so admin can select one author for an article. I have something like this:

<select class="form-control select2" id="article-author" name="author">
    <option value="0" {% if article.author == "0" %}selected{% endif %}>- Select author -</option>
    {% for user in users %}
        <option value="{{ user.id }}" {% if article.author == user.id %}selected{% endif %} >{{ user.username }}</option>
    {% endfor %}
 </select>

This code seems to fail on if statement inside the for loop. Removing it, makes template to render. I know this is kinda stupid error, but I'm looking at it for over half an hour, and I can't find my mistake. Could you, please, point it out?

PS. I have a feeling that code-block marking syntax does not work properly here, so here is an gist - https://gist.github.com/LMSzor/57ab3f5be72277c8530779ad56b0f795

One more disovery: I'm using same template for both creating and editing and article by creating empty $this->view->setVar('article', new Article());, and whole template renders perfectly on this page. It seems to only crash while editing an article. It also seems, to crash when article.author is not empty, and not when if returns true (I found this one out by assigning user of id 4 to author field of article - if it would crash on if returning true, then I would have few users listed in page source before the crash).



1.6k

Have you tried something like this?

{{ select("author", users, class: "form-control select2", using: ["id", "username"], useEmpty: true, emptyText: "- Select author - ", emptyValue: "0", value: article.author) }}

Are there any error messages? Does the remaining site render completly?

Check your logs :)



808
Accepted
answer

Funny thing. My roommate, who is also a programmer, came from work and told ne to look on my model. Apparently i had relation alias and column with the same name, and that was a problem. Funniest of all is that he has no experience with Phalcon.

CLOSED, sorry for making a fuss out of nothing. :(