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

For loop variable being compiled incorrectly

The url variable inside the loop is being compiled to $this->url. I've only noticed it doing this when trying to use the variable name url.

<urlset>
    {% for url in urls %}
    <url>
        <loc>{{ url.loc }}</loc>
        <lastmod>{{ url.lastmod }}</lastmod>
        <changefreq>{{ url.changefreq }}</changefreq>
        <priority>{{ url.priority }}</priority>
    </url>
    {% endfor %}
</urlset>

Compiles to:

<urlset>
    <?php foreach ($urls as $url) { ?>
    <url>
        <loc><?php echo $this->url->loc; ?></loc>
        <lastmod><?php echo $this->url->lastmod; ?></lastmod>
        <changefreq><?php echo $this->url->changefreq; ?></changefreq>
        <priority><?php echo $this->url->priority; ?></priority>
    </url>
    <?php } ?>
</urlset>


98.9k

Variables are first checked if their name is a service registered in the application, in this case, "url" is the url resolver service, you will have the same behavior if you use: "di", "view", "router", "dispatcher", "flash", "session", "modelsManager", etc.

https://docs.phalcon.io/en/latest/reference/volt.html#inject-services-into-a-template