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 %}