What is the best way to format a button macro? I'm looking for a solution on the best way to setup a macro for buttons that are defined in a matrix fieldtype.

My current code works fine but it really isn't that DRY and I feel it could be made better.

I first import my button macro in a main template:

{% import "_includes/buttons" as buttons %}


Then my code for the matrix field is:

{% for block in cube.button %}
   {{ buttons.span(block.buttonType, block.buttonText, block.buttonColour, block.buttonSize, block.fullWidthButton, cube.linkUrl) }}
{% endfor %}


And now my not so DRY button macro

{% macro span(type, text, colour, size, fullWidth, url) -%}
    &lt;span class="btn {{ type }} {{ colour }} {{ size }}{% if fullWidth %} btn-block{% endif %}"&gt;{{ text }}&lt;/span&gt;
{% endmacro -%}

{% macro a(type, text, colour, size, fullWidth, url) -%}
    &lt;a href="{{ url }}" class="btn {{ type }} {{ colour }} {{ size }}{% if fullWidth %} btn-block{% endif %}"&gt;{{ text }}&lt;/a&gt;
{% endmacro -%}

{% macro button(type, text, colour, size, fullWidth, url) -%}
    &lt;button class="btn {{ type }} {{ colour }} {{ size }}{% if fullWidth %} btn-block{% endif %}"&gt;{{ text }}&lt;/button&gt;
{% endmacro -%}

{% macro submit(type, text, colour, size, fullWidth, url) -%}
    &lt;input type="submit" class="btn {{ type }} {{ colour }} {{ size }}{% if fullWidth %} btn-block{% endif %}" value="{{ text }}"/&gt;
{% endmacro -%}
You could factor out that repeated class=... like so:

{% macro _btnClass(type, colour, size, fullWidth) %}
    class="btn {{ type }} {{ colour }} {{ size }}{% if fullWidth %} btn-block{% endif %}"
{% endmacro %}

{% macro span(type, text, colour, size, fullWidth, url) -%}
{% import _self as self %}
    &lt;span {{ self._btnClass(type, colour, size, fullWidth) }}&gt;{{ text }}&lt;/span&gt;
{% endmacro -%}


It is not clear from your question how you decide which buttons macro to call. Perhaps that code needs to be made DRYer.