Body: <p>I currently have the following code as part of the address form in Craft Commerce. </p>
  
  <pre><code>        &lt;label class="label" for="{{ modelName }}-firstName"&gt;First Name (*)&lt;/label&gt;
          &lt;input type="text" id="{{ modelName }}-firstName" {% if model and model.getErrors('firstName') %}class="has-error"{% endif %} name="{{ modelName }}[firstName]" value="{{ model ? model.firstName : '' }}"&gt;
          {% if model and model.getErrors('firstName') %}
              &lt;span class="has-error"&gt;
                  &lt;svg role="img" class="icon" title="Error Icon"&gt;
                      &lt;use xlink:href="/assets/img/icons.svg#icon-error"&gt;&lt;/use&gt;
                  &lt;/svg&gt;
                  {{ model.getError('firstName') }}
              &lt;/span&gt;
          {% endif %}
  </code></pre>
  
  <p>This repeats several times for each form item (surname, address1, address2, postcode, country etc).</p>
  
  <p>I also want to reuse the error code part for other areas on the website, when errors occur, be this discount code box, login form etc.</p>
  
  <pre><code>&lt;span class="has-error"&gt;
      &lt;svg role="img" class="icon" title="Error Icon"&gt;
          &lt;use xlink:href="/assets/img/icons.svg#icon-error"&gt;&lt;/use&gt;
      &lt;/svg&gt;
      {{ model.getError('firstName') }} //This part will change based on application.
  &lt;/span&gt;
  </code></pre>
  
  <p>My question is how can I extract this part to ensure it is DRYer and not repeat it over and over? So if I want to change the class, the icon etc, I won't have loads of separate pieces of code to update.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-04T20:17:38.493Z
Comments.Text: Sounds like you're looking for Twig macros? http://twig.sensiolabs.org/doc/tags/macro.html
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-23T13:50:55.630Z
Comments.Text: Any ideas on how I should structure this as the errors are all different types. See this article -> http://craftcms.stackexchange.com/questions/16188/getting-to-grips-with-errors-and-flash-messages
Comments.UserDisplayName: Terry Upton
Tags: <plugin-craftcommerce><best-practice><dry>
Title: Error Icon and DRYer Code
ThreadText: # Error Icon and DRYer Code
  
  (Asked by: username_0 on 2016-08-04)
  
  I currently have the following code as part of the address form in Craft Commerce.
  
  ```
          <label class="label" for="{{ modelName }}-firstName">First Name (*)</label>
          <input type="text" id="{{ modelName }}-firstName" {% if model and model.getErrors('firstName') %}class="has-error"{% endif %} name="{{ modelName }}[firstName]" value="{{ model ? model.firstName : '' }}">
          {% if model and model.getErrors('firstName') %}
              <span class="has-error">
                  <svg role="img" class="icon" title="Error Icon">
                      <use xlink:href="/assets/img/icons.svg#icon-error"></use>
                  </svg>
                  {{ model.getError('firstName') }}
              </span>
          {% endif %}
  
  ```
  
  This repeats several times for each form item (surname, address1, address2, postcode, country etc).
  
  I also want to reuse the error code part for other areas on the website, when errors occur, be this discount code box, login form etc.
  
  ```
  <span class="has-error">
      <svg role="img" class="icon" title="Error Icon">
          <use xlink:href="/assets/img/icons.svg#icon-error"></use>
      </svg>
      {{ model.getError('firstName') }} //This part will change based on application.
  </span>
  
  ```
  
  My question is how can I extract this part to ensure it is DRYer and not repeat it over and over? So if I want to change the class, the icon etc, I won't have loads of separate pieces of code to update.
  
  **Tags:** plugin-craftcommerce, best-practice, dry
  
  > **Comments:**
  > **username_1**: Sounds like you're looking for Twig macros? http://twig.sensiolabs.org/doc/tags/macro.html  
  > **username_0**: Any ideas on how I should structure this as the errors are all different types. See this article -> http://craftcms.stackexchange.com/questions/16188/getting-to-grips-with-errors-and-flash-messages  
  
  ---
  
  ## 0 answers
  
  