1

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

<select name="country" required>
{% for country in craft.i18n.getLocaleData().getAllTerritories() %}
    <option>{{ country }}</option>
{% endfor %}</select>

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?

  • I think it should be {{ country.name }} now it wont output anything good if im right. – Justin Dekkers Mar 6 '17 at 11:17
  • Yeah that throws an error. – Ian Ebden Mar 6 '17 at 11:28
1

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:

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

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:

<select name="country" required>
    {% for country in craft.territories.getTerritories() %}
        <option>{{ country }}</option>
    {% endfor %}
</select>
  • Thanks Steve. I get a preg_match(): Unknown modifier '{' error. Been Googling around but I can't figure it out. – Ian Ebden Mar 6 '17 at 15:51
  • Ha! Looks like I was trying to be too clever with the regex. Try the amended version to see if that works. ;-) {% if (key matches '[a-z][a-z]') and (key != 'zz') %} – Steve Rowling Mar 6 '17 at 15:53
  • Hehe, nearly there! preg_match(): Unknown modifier '[' – Ian Ebden Mar 6 '17 at 15:55
  • Oops, looks like I forgot to enclose the pattern in slashes. This should work now: {% if (key matches '/[a-z][a-z]/') and (key != 'zz') %} – Steve Rowling Mar 6 '17 at 15:57
  • Thnx. Well, no errors this time but no output either. – Ian Ebden Mar 6 '17 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.