Reputation: 467
What I'm trying to do is include / remove a piece of markup based on an if statement, using a twig template. This seems simple enough, but I'm very new to PHP as a whole (and twig) and am not sure why this isn't working...
{% set regions = ["en", "en-euw", "de", "es", "fr", "it", "en-eune", "pl", "ro", "el", "pt-br", "tr", "ru", "mx" ] %}
{% if {{ locale.url_code }} is iterable %}
<p>lol bro</p>
{% endif %}
First off, {{ locale.url_code }} will print one of the language codes within the array, but there are many more than those listed in the array.
Basically I'm trying to set an array that contains the appropriate language/region codes, and if {{ locale.url_code }} is inside the "regions" array --show the markup in the if statement. I'm not totally sure I get how iterable works...but I think that's going to be key in making this work. I also am not 100% sure I'm setting the array up correctly...
Any help or direction is much appreciated.
Thanks!
Upvotes: 0
Views: 50
Reputation: 467
The answer above was a great starting point, but I wasn't able to get in_array to work within a twig template. Luckily after scanning some twig documentation I saw the rules about containment operators which allow me to do exactly what I need to do, in an oddly ruby-like syntax. See the working code below:
{% set regions = ['en', 'en-euw', 'de', 'es', 'fr', 'it', 'en-eune', 'pl', 'ro', 'el', 'pt-br', 'tr', 'ru', 'mx' ] %}
{% if locale.url_code in regions %}
<p>lol bro</p>
{% endif %}
Hopefully this helps anyone looking for a way to do this.
Upvotes: 2