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

Calling dynamic named macros

Hi everyone,

my goal is to render small blocks of html inside of a for loop. My idea was, to define a macro which then can be used in my for loop. The problem is, that the name of the macro is part of the loop data.

For example I define a macro like this:

{% macro myMacroTitle() %}
Here comes the html content.
{% endmacro %}

Later I want to display the content of the macro like this:

{% for name, element in myelements %}
    This is the content of {{ name }}:
    {% element.macroTitle() %}
{% endfor %}

Is it possible to call a macro with a title held in a variable? Or is there some other way (not partials in separate files) to render content with a name stored in a variable?

Thans in advance,

HansPeter

I don't think this is possible. Perhaps rewrite your macro to accept properties from element, so it can still render dynamic output.