I want to create a User Agent check. (Yes I know. It is evil.)
So I created an Array. The whole idea is to see if the user agent contains a specific word. This way I hope to circumvent regex trickery. But I have a hard time figuring how to return true
only once:
This would be the array:
{% set browserValues = [ { agent: 'android'}, { agent: 'iemobile'} {# and so on #}]
This is how I get the user agent:
{% set userAgent = craft.request.getUserAgent()|lower %}
I came this far:
{% for item in browserValues %}
{% if item.agent in userAgent %}
You're on mobile
{% else %}
You're not on mobile
{% endif %}
{% endfor %}
This actually works. But there are two problems: If the conditional is false (not on mobile), the conditional is applied for each key in the array. You're not on mobile You're not on mobile You're not on mobile
If the user agent string contains the word android
twice the conditional is applied twice.
How can I prevent the conditional from looping over and over? (My brain starts to loop over and over too).
Thank you.