<p>Unlike in PHP, it's not actually possible to break a <code>for</code> loop in Twig.</p>

<p>To work around that fact, you can use the <code>for</code> loop to set a flag, and then put the conditional outside the loop:</p>

<pre><code>{% set isUserOnMobile = false %}

{% for item in browserValues %}
    {% set isUserOnMobile = not isUserOnMobile ? item.agent in userAgent : isUserOnMobile %}
{% endfor %}

{% if isUserOnMobile %}
    You're on mobile
{% else %}
    You're not on mobile
{% endif %}
</code></pre>

<p>Another option: I actually wrote a plugin called <a href="https://github.com/mmikkel/Detect-Craft">Detect</a> for device detection (the plugin wraps the excellent <a href="http://mobiledetect.net/">Mobile_Detect</a> library, which is sponsored by BrowserStack). Might be you'll be be able to use that, instead of hard coding lots of user agent strings. Here's how you can use Detect in a template:</p>

<pre><code>{% set isUserMobile = craft.detect.isMobile %}
{% set isUserTablet = craft.detect.isTablet %}
{% set isUserPhone = craft.detect.isPhone %}
{% set isUserIphone = craft.detect.is('iphone') %}
{% set userAgent = craft.detect.getUserAgent %}
</code></pre>

CC BY-SA 3.0
2016-02-09T11:28:02.327Z
+1 for using a plugin for this problem.
carlcs
CC BY-SA 4.0
2019-11-21T19:24:26.087Z
Is there a way to check if this is an iPhoneX (as opposed to other versions of iPhone)? I tried {{ craft.mobileDetect.version('iPhone') }} but just get a 1. Thanks.
userAW
CC BY-SA 3.0
2016-02-09T11:25:55.920Z
2016-02-09T11:29:59.060Z
2016-02-09T11:29:59.060Z
Mats Mikkel Rummelhoff
<p>I want to create a User Agent check. (Yes I know. It is evil.)</p>

<p>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 <code>true</code> only once:</p>

<p>This would be the array:</p>

<pre><code>{% set browserValues = [ { agent: 'android'}, { agent: 'iemobile'} {# and so on #}]
</code></pre>

<p>This is how I get the user agent:</p>

<pre><code>{% set userAgent = craft.request.getUserAgent()|lower %}
</code></pre>

<p>I came this far:</p>

<pre><code> {% for item in browserValues %}
   {% if item.agent in userAgent %}
     You're on mobile
   {% else %}
     You're not on mobile
   {% endif %}
 {% endfor %}
</code></pre>

<p>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. <code>You're not on mobile You're not on mobile You're not on mobile</code></p>

<p>If the user agent string contains the word <code>android</code> twice the conditional is applied twice.</p>

<p>How can I prevent the conditional from looping over and over? (My brain starts to loop over and over too).</p>

<p>Thank you.</p>

<conditional><array><contains>
Conditional to detect mobile devices with getUserAgent?
# Conditional to detect mobile devices with getUserAgent?

(Asked by: username_0 on 2016-02-09)

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.

**Tags:** conditional, array, contains


---

## 1 answers

### Answer by username_1 on 2016-02-09. Score: 6 | Accepted answer

Unlike in PHP, it's not actually possible to break a for loop in Twig.
To work around that fact, you can use the for loop to set a flag, and then put the conditional outside the loop:
{% set isUserOnMobile = false %}

{% for item in browserValues %}
    {% set isUserOnMobile = not isUserOnMobile ? item.agent in userAgent : isUserOnMobile %}
{% endfor %}

{% if isUserOnMobile %}
    You're on mobile
{% else %}
    You're not on mobile
{% endif %}

Another option: I actually wrote a plugin called Detect for device detection (the plugin wraps the excellent Mobile_Detect library, which is sponsored by BrowserStack). Might be you'll be be able to use that, instead of hard coding lots of user agent strings. Here's how you can use Detect in a template:
{% set isUserMobile = craft.detect.isMobile %}
{% set isUserTablet = craft.detect.isTablet %}
{% set isUserPhone = craft.detect.isPhone %}
{% set isUserIphone = craft.detect.is('iphone') %}
{% set userAgent = craft.detect.getUserAgent %}

> **Comments:**
> **username_2**: +1 for using a plugin for this problem.  
> **username_3**: Is there a way to check if this is an iPhoneX (as opposed to other versions of iPhone)? I tried {{ craft.mobileDetect.version('iPhone') }} but just get a 1. Thanks.  

---

