Templating - Get a string of the entry's field handle I'm passing entry fields into a macro and I'd like to be able to get a string of the field handle

myMacro(entry.field1)
myMacro(entry.field2)

{% macro myMacro(entry) %}
  {# I want a string of 'field1' here #}
{% endmacro %}
You won't be able to do it that way because entry.field2 will return you the normalized field value and there is no way to trace back some object|string|variable to an unknown entry with an unknown field handle. You need to pass the handle as a second parameter in your macro

{% macro myMacro(field, fieldHandle) %}
    {{ fieldHandle }}
{% endmacro %}

myMacro(entry.field2, 'field2').


You could also pass the entry and a field handle instead of the field

{% macro myMacro(element, fieldHandle) %}
    {{ fieldHandle }}
    {% set value = element.getFieldValue(fieldHandle) %}
{% endmacro %}

myMacro(entry, 'field2').