<p>You could factor out that repeated <code>class=...</code> like so:</p>

<pre><code>{% 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 -%}
</code></pre>

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

CC BY-SA 3.0
2014-12-29T22:16:36.147Z
Updated question with the macro import: which is just one file that holds all the different macros.
since1976
CC BY-SA 3.0
2014-12-30T00:15:45.517Z
You must be calling buttons.a and buttons.button and buttons.submit somehow, but I'm not sure how.
Marion Newlevant
CC BY-SA 3.0
2014-12-30T00:41:01.527Z
Ah you are absolutely right. Thought I would be able to set that as a var and use that. Turns out that doesn't work. :(
since1976
CC BY-SA 3.0
2014-12-29T21:54:47.700Z
2014-12-29T21:54:47.700Z
Marion Newlevant
<p>I'm looking for a solution on the best way to setup a macro for buttons that are defined in a matrix fieldtype.</p>

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

<p>I first import my button macro in a main template:</p>

<pre><code>{% import "_includes/buttons" as buttons %}
</code></pre>

<p>Then my code for the matrix field is:</p>

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

<p>And now my not so DRY button macro</p>

<pre><code>{% 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 -%}
</code></pre>

<macro>
What is the best way to format a button macro?
# What is the best way to format a button macro?

(Asked by: username_0 on 2014-12-29)

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) -%}
    {{ text }}
{% endmacro -%}

{% macro a(type, text, colour, size, fullWidth, url) -%}
    {{ text }}
{% endmacro -%}

{% macro button(type, text, colour, size, fullWidth, url) -%}
    {{ text }}
{% endmacro -%}

{% macro submit(type, text, colour, size, fullWidth, url) -%}
    
{% endmacro -%}

**Tags:** macro


---

## 1 answers

### Answer by username_1 on 2014-12-29. Score: 2

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 %}
    {{ text }}
{% endmacro -%}

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

> **Comments:**
> **username_0**: Updated question with the macro import: which is just one file that holds all the different macros.  
> **username_1**: You must be calling buttons.a and buttons.button and buttons.submit somehow, but I'm not sure how.  
> **username_0**: Ah you are absolutely right. Thought I would be able to set that as a var and use that. Turns out that doesn't work. :(  

---

