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.

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) %}
        {% for id in ids %}
            <option value = "{{id}}">{{id}}</option>
        {% endfor %}
{%- 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.