1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can I get part of the $userAgent variable

Discussion in 'XenForo Development Discussions' started by AndyB, Nov 20, 2013.

  1. AndyB

    AndyB Well-Known Member

    I would like to see if the $session.userAgent variable contains the word "iPad".

    Currently I have the following code in the PAGE_CONTAINER template and it works fine:

    Code:
    <xen:if is="{$session.userAgent} == 'Mozilla/5.0 (iPad; CPU OS 7_0_3 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B511 Safari/9537.53'">  
    <xen:comment>ipad</xen:comment>
    <xen:else />
    <xen:include template="page_container_js_head" />
    </xen:if>
    
    How would I use xen:string or xen:helper or any other command to get part of the $session.userAgent variable?

    Thank you.
     
    Last edited: Nov 20, 2013
  2. xf_phantom

    xf_phantom Well-Known Member

    AFAIK you can't do this inside the template

    A possible solution would be to validate this on event visitor_setup and attach your informations to the visitor object!
    Then you could use if is="{$visitor.isIpad}">...</ inside the templates.
     
    Last edited: Nov 20, 2013
  3. AndyB

    AndyB Well-Known Member

    I got it! This works perfectly.

    Code:
    <xen:if is="{xen:helper wordtrim, $session.userAgent, 20, 'iPad'} == 'Mozilla/5.0 (iPad;...'">  
    <xen:comment>iPad</xen:comment>
    <xen:else />
    <xen:include template="page_container_js_head" />
    </xen:if>
    
     

Share This Page