Adding to default classes in a macro I'm fairly new to using macros and haven't found anything online regarding this — sorry if this is a possible duplicate post.

I've created a macro with 7 possible parameters but on different templates I want to add to the default classes.

{% macro button(label, link, width , classes, colours, textclasses, alignment) %}
  &lt;a href="{{ link }}" class="btn {{ width|default('w5') }} {{ colours|default('bg-tdgreen white') }} {{ classes|default('flex pa2 pa3-ns ph3 tc lh-solid f7 f6-ns link underline-hover z-999 br2 mv3 tracked-more') }} {{ alignment|default('center') }}"&gt;
    &lt;p class="ttu center mv0 {{ textclasses|default('') }}"&gt;{{ label }}&lt;/p&gt;
  &lt;/a&gt;
{% endmacro %}


Is there a way of doing this without having to set a variable with all the defaults in it and tacking on new classes? For example:

{% set buttonClasses = "absolute w5-ns l-50 tc pa2 pa3-ns ph3 br2 z-max link white f6 flex mt--18 mt--25-ns underline-hover tracked-more" %}
  {% if buttonPosition == 'above' %}
    {% set buttonClasses = buttonClasses ~ " top-0" %}
  {% endif %}
{{ macros.button(block.ctaText, block.ctaLink.one().url, "", buttonClasses) }}


Any help would be appreciated!

— Thanks
Tons of ways to skin this cat; a simple option is to use string concatenation, which could look like the below example. Note that I'm actually creating a new variable for classes at the top of the macro, which IMO helps clean up the markup a little bit and also makes it a bit more clear what's going on – you can continue to do this logic "inline" if you want to, of course.

Also note that the default filter has been omitted from the Twig statement rendering the classes variable; it's redundant now because the classes variable will always be set.

{% macro button(label, link, width, classes, colours, textclasses, alignment) %}
    {% set classes = 'flex pa2 pa3-ns ph3 tc lh-solid f7 f6-ns link underline-hover z-999 br2 mv3 tracked-moreflex pa2 pa3-ns ph3 tc lh-solid f7 f6-ns link underline-hover z-999 br2 mv3 tracked-more' ~ (classes ? ' ' ~ classes) %}
    &lt;a href="{{ link }}" class="btn {{ width|default('w5') }} {{ colours|default('bg-tdgreen white') }} {{ classes }} {{ alignment|default('center') }}"&gt;
        &lt;p class="ttu center mv0 {{ textclasses|default('') }}"&gt;{{ label }}&lt;/p&gt;
    &lt;/a&gt;
{% endmacro %}


The important bit is the ... ~ (classes ? classes) part, which basically appends the classes macro parameter to the default classnames, if the parameter is set.

A variation on the above solution, and – in my opinion – maybe a bit cleaner, is to use an array that is joined (i.e. flattened to a string):

{% macro button(label, link, width, classes, colours, textclasses, alignment) %}{% macro button(label, link, width, classes, colours, textclasses, alignment) %}
    {% set classes = ['flex pa2 pa3-ns ph3 tc lh-solid f7 f6-ns link underline-hover z-999 br2 mv3 tracked-moreflex pa2 pa3-ns ph3 tc lh-solid f7 f6-ns link underline-hover z-999 br2 mv3 tracked-more', classes]|filter|join(' ') %}
    ...
{% endmacro %}


Basically, in the above the classes variable is an array containing the default classes and whatever was passed in the classes parameter. The array is piped through the filter Twig filter, which removes any null/undefined values (i.e. it removes the classes parameter value if one wasn't set), and finally join(' ') which merges the two array values together with a space (which means it's now a string and you can do {{ classes }} to print the whole thing).