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

include view dynamically

I'm trying to iterate through a Model collection in volt:

{% if model.elements|length > 0 %}
    {% for element in model.getElements() %}
        {% include "partials/panels/edit-" ~ element.getType() ~ ".volt" %}
    {% endfor %}
{% endif %}

The type can be text or images. If i use the above code, i get the error:

View "/path/to/phalcon/apps/frontend/views/partials/panels/edit-image.volt" was not found in the views directory

I'm sure that the file exists, since if i changethe include, it'll work:

{% include "partials/panels/edit-image.volt" %}

It'll also fail on:

{% include "partials/pandels/edit-" ~ "image.volt %} 

What is the reason that the first version is producing that error? ( I know i could just use ifs.. But theres quite a list of element types later on. )



58.4k
Accepted
answer

Hey man

Can you used property partial is included

    {% include "partials/panels/edit-" ~ element.getType() ~ ".volt" %}

Replace to

    {{ partial( "partials/panels/edit-" ~ element.getType())  }}

Thanks, that worked. I'd still like to know "why" the first implementation did not work though. I don't see the actual difference here ..



58.4k

May be this is bug, you can create issue on github:)