<p>I think you're looking for the attribute function:</p>

<pre><code>{% set field = attribute(addressFields, key) %}
{{ field.label }}
</code></pre>

CC BY-SA 3.0
2015-12-15T21:27:32.897Z
2015-12-15T21:27:32.897Z
Luke Holder
<p>I need to be able to assign and output my own custom labels for the fields that are in the shipping/billing addresses during checkout in Craft Commerce. </p>

<p>I've already done so on the form like so:</p>

<pre><code>{% set countries = craft.commerce.countriesList %}
{% set states = craft.commerce.statesArray %}
{% set addressFields = {
    firstName: {
        label: 'First Name',
        required: 1,
    },
    lastName: {
        label: 'Last Name',
        required: 1,
    },
    address1: {
        label: 'Address',
        required: 1,
    },
    address2: {
        label: 'Address 2',
        required: 1,
    },
    city: {
        label: 'City',
        required: 1,
    },
    zipCode: {
        label: 'Zip Code',
        required: 1,
    },
    phone: {
        label: 'Phone',
        required: 1,
    },
    alternativePhone: {
        label: 'Alternative Phone',
        required: 1,
    },
} %}


{% set modelName = modelName is defined ? modelName : 'address' %}
{% set model = address is defined ? address : null %}

&lt;div class="addressBox {{ modelName }}"&gt;
    {% for key,options in addressFields %}
        &lt;div class="form-group col-md-6 {% if model and model.getErrors(key) %}has-error{% endif %}"&gt;
            &lt;label for="{{ modelName }}-{{ key }}"&gt;{{ options.label|t }}{{ options.required ? '*' : '' }}&lt;/label&gt;
            &lt;input type="text" id="{{ modelName }}-{{ key }}" class="u-full-width" name="{{ modelName }}[{{ key }}]"
                   value="{{ model ? model[key] : '' }}"&gt;
            {% if model and model.getErrors(key) %}
                &lt;span class="flash alert-warning"&gt;{{ model.getErrors(key)|join }}&lt;/span&gt;
            {% endif %}
        &lt;/div&gt;
    {% endfor %}
</code></pre>

<p>That works like a charm. I need to however also be able to output it with my custom labels as well. Here is my currently template area, i'm not sure how i can get my custom labels in here. Please advise:</p>

<pre><code>{% for address in addresses %}
     {% for key,value in address.attributes() %}

          {% switch key %}

               {% case "id" %} {# ingore #}
               {% case "customerId" %} {# ingore #}

               {% default %}

               {% if value %}
                    &lt;li&gt;
                         &lt;h3 class="cart-product-details"&gt;{{ key|title }}:
                              &lt;span class="address-option" data-key="{{ key }}" data-value="{{ value }}"&gt;
                                   {{ key == 'countryId' ? address.countryText : (key == 'stateId' ? address.stateText : value) }}
                              &lt;/span&gt;
                        &lt;/h3&gt;
                    &lt;/li&gt;
               {% endif %}

          {% endswitch %}

     {% endfor %}
</code></pre>

<form><ecommerce><plugin-craftcommerce><label><shipping>
Custom Labels for Address Fields Craft Commerce
# Custom Labels for Address Fields Craft Commerce

(Asked by: username_0 on 2015-12-15)

I need to be able to assign and output my own custom labels for the fields that are in the shipping/billing addresses during checkout in Craft Commerce. 
I've already done so on the form like so:
{% set countries = craft.commerce.countriesList %}
{% set states = craft.commerce.statesArray %}
{% set addressFields = {
    firstName: {
        label: 'First Name',
        required: 1,
    },
    lastName: {
        label: 'Last Name',
        required: 1,
    },
    address1: {
        label: 'Address',
        required: 1,
    },
    address2: {
        label: 'Address 2',
        required: 1,
    },
    city: {
        label: 'City',
        required: 1,
    },
    zipCode: {
        label: 'Zip Code',
        required: 1,
    },
    phone: {
        label: 'Phone',
        required: 1,
    },
    alternativePhone: {
        label: 'Alternative Phone',
        required: 1,
    },
} %}


{% set modelName = modelName is defined ? modelName : 'address' %}
{% set model = address is defined ? address : null %}


    {% for key,options in addressFields %}
        
            {{ options.label|t }}{{ options.required ? '*' : '' }}
            
            {% if model and model.getErrors(key) %}
                {{ model.getErrors(key)|join }}
            {% endif %}
        
    {% endfor %}

That works like a charm. I need to however also be able to output it with my custom labels as well. Here is my currently template area, i'm not sure how i can get my custom labels in here. Please advise:
{% for address in addresses %}
     {% for key,value in address.attributes() %}

          {% switch key %}

               {% case "id" %} {# ingore #}
               {% case "customerId" %} {# ingore #}

               {% default %}

               {% if value %}
                    
                         {{ key|title }}:
                              
                                   {{ key == 'countryId' ? address.countryText : (key == 'stateId' ? address.stateText : value) }}
                              
                        
                    
               {% endif %}

          {% endswitch %}

     {% endfor %}

**Tags:** form, ecommerce, plugin-craftcommerce, label, shipping


---

## 1 answers

### Answer by username_1 on 2015-12-15. Score: 2 | Accepted answer

I think you're looking for the attribute function:
{% set field = attribute(addressFields, key) %}
{{ field.label }}


---

