Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-29T22:16:36.147Z
Answers.Text: Updated question with the macro import: which is just one file that holds all the different macros.
Answers.UserDisplayName: since1976
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-30T00:15:45.517Z
Answers.Text: You must be calling buttons.a and buttons.button and buttons.submit somehow, but I'm not sure how.
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-30T00:41:01.527Z
Answers.Text: 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. :(
Answers.UserDisplayName: since1976
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-29T21:54:47.700Z
Answers.LastActivityDate: 2014-12-29T21:54:47.700Z
Answers.UserDisplayName: Marion Newlevant
Body: <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>
  
Tags: <macro>
Title: What is the best way to format a button macro?
ThreadText: # 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) -%}
      <span class="btn {{ type }} {{ colour }} {{ size }}{% if fullWidth %} btn-block{% endif %}">{{ text }}</span>
  {% endmacro -%}
  
  {% macro a(type, text, colour, size, fullWidth, url) -%}
      <a href="{{ url }}" class="btn {{ type }} {{ colour }} {{ size }}{% if fullWidth %} btn-block{% endif %}">{{ text }}</a>
  {% endmacro -%}
  
  {% macro button(type, text, colour, size, fullWidth, url) -%}
      <button class="btn {{ type }} {{ colour }} {{ size }}{% if fullWidth %} btn-block{% endif %}">{{ text }}</button>
  {% endmacro -%}
  
  {% macro submit(type, text, colour, size, fullWidth, url) -%}
      <input type="submit" class="btn {{ type }} {{ colour }} {{ size }}{% if fullWidth %} btn-block{% endif %}" value="{{ text }}"/>
  {% 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 %}
      <span {{ self._btnClass(type, colour, size, fullWidth) }}>{{ text }}</span>
  {% 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. :(  
  
  ---
  
  