<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>

CC BY-SA 3.0
2016-08-04T20:17:38.493Z
Sounds like you're looking for Twig macros? http://twig.sensiolabs.org/doc/tags/macro.html
Brad Bell
CC BY-SA 3.0
2016-08-23T13:50:55.630Z
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
Terry Upton
<plugin-craftcommerce><best-practice><dry>
Error Icon and DRYer Code
# 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. 
        First Name (*)
        
        {% if model and model.getErrors('firstName') %}
            
                
                    
                
                {{ model.getError('firstName') }}
            
        {% 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.

    
        
    
    {{ model.getError('firstName') }} //This part will change based on application.


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

