elivz / VzAddress-Craft

Address fieldtype for the Craft CMS
30 stars 13 forks source link

Get country code list from template #27

Closed tobystokes closed 7 years ago

tobystokes commented 7 years ago

Great plugin (I use it's dadEE too!)

Is it possible to get a full list of all the country codes in a twig front end template?

I'm trying to make a form to search for addresses, and while the following generates the correct output:

{% set countryCodes = ['AF', 'AX', 'AL', 'DZ', '..........', 'ZM', 'ZW'] %}
{% set localeData = craft.i18n.getLocaleData() %}
<select name="country">
  {% for country in countryCodes %}
    <option value="{{ country }}">{{ localeData.getTerritory(country) }}</option>
  {% endfor %}
</select>

I'd rather not duplicate that entire country list from your fieldtype.

elivz commented 7 years ago

That's a very good idea, and should be easy to implement. I will try to get a release out with that addition in the next day or two.

elivz commented 7 years ago

This is added in v1.5.0. You can access the array of countries using craft.vzAddress.countries. So your example above would become:

<select name="country">
    {% for countryCode, countryName in craft.vzAddress.countries %}
        <option value="{{ countryCode }}">{{ countryName }}</option>
    {% endfor %}
</select>
tobystokes commented 7 years ago

nice!