Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Volt Custom Function not working?

I am using this to create a custom function 'file_exists'.

$volt->getCompiler()->addFunction('file_exists', function($file) {
        return file_exists('http://localhost/balloon/' . $file);
});

When I try to call the function in an if statement, the compiled template is blank (if statement).

{% if file_exists("public/images/edited/" ~ picture.position ~ "/" ~ session.get('username') ~ "_" ~ picture.text|url_encode ~ "." ~ picture.extension) %}
{% endif %}


83.4k
Accepted
answer

Actually, a Volt function must not execute code directly but it has to return "code" that is inserted as part of the generated code:

$volt->getCompiler()->addFunction('file_exists', function($resolvedArgs) {
    return "file_exists('http://localhost/balloon/'" . $resolvedArgs . ")";
});

http://docs.phalconphp.com/en/latest/reference/volt.html#id1