Locale-specific country select menu Wonder if you can help? Got a booking form on a multi-locale site and I need to include the user's country in the form. I'm using Craft's LocaleData class to output locale-specific locales (e.g. Angleterre for England)...

&lt;select name="country" required&gt;
{% for country in craft.i18n.getLocaleData().getAllTerritories() %}
    &lt;option&gt;{{ country }}&lt;/option&gt;
{% endfor %}&lt;/select&gt;


Works great except it outputs stuff I don't need like "World", "Oceana", "Ascension Island" etc... Is there any way to control the output, like maybe show countries only and not regions?
If you look at the data file for a locale in Yii you can see that the territories property includes regions and continents as you've found. These all have keys with numeric values, whereas countries have two character keys. Using this knowledge, you can loop over the keys and values of the territories array as you are doing, but only output those with two character keys. You'll also probably want to omit the Unknown Region value which has a key of zz.

Untested, but this should work:

&lt;select name="country" required&gt;
    {% for key, value in craft.i18n.getLocaleData().getAllTerritories() %}
        {% if (key matches '/[a-z][a-z]/') and (key != 'zz') %}
            &lt;option&gt;{{ value }}&lt;/option&gt;
        {% endif %}
    {% endfor %}
&lt;/select&gt;




EDIT

OK, so my original idea doesn't work as craft.i18n.getLocaleData().getAllTerritories() doesn't preserve the territory array keys from the Yii i18n data files.

I'm not sure you can do what you want to do natively in Craft, but it looks like John D. Wells' Territories fieldtype plugin may provide what you need. You don't need to use the fieldtype as there is an undocumented Twig variable provided that gives you an array of localised territory names, excluding the regions. Try this:

&lt;select name="country" required&gt;
    {% for country in craft.territories.getTerritories() %}
        &lt;option&gt;{{ country }}&lt;/option&gt;
    {% endfor %}
&lt;/select&gt;