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

Capturing macro/partial output

I have to render a dropdown multiple times for the same user. Rather than call a macro or partial() every time, I'd like to run a macro once, capture the output, then repeatedly output what was captured.

Something like:

{%- macro dropdown(ids) %}
    <select>
        {% for id in ids %}
            <option value = "{{id}}">{{id}}</option>
        {% endfor %}
    </select
{%- endmacro %}

{% set idSelect = dropdown([1,2,3]) %}

{{ idSelect }}
{{ idSelect }}

However, when compiled to PHP, the call to the dropdown macro causes it to generate output, rather than return it.

I know I could use raw PHP code & output buffering, but I was wondering if there was a cleaner or Volt-native way to do this.

Hmmm, yep - that's much nicer. Thanks.