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

XF 1.2 Randomise between two elements to include in a template?

Discussion in 'XenForo Questions and Support' started by CyclingTribe, Nov 25, 2013.

  1. CyclingTribe

    CyclingTribe Well-Known Member

    I'm looking to migrate from one revenue program to another, but as the newer one requires the building of several months of click-tracking before payout, I want to slowly migrate over by using the newer code on a smaller scale to begin with, increasing coverage over the following months; presenting the new code, say (20%) of the time in the first month, 40% in the next, 60% the month after, etc.

    I wondered if there was an easy way to do this via the XF template code? Maybe generate a random number between 1-100 and if the number is <= 25 put the new code in, else present the current code?

    Does XF have a random function for templates?

    Cheers,
    Shaun :D
     
  2. AndyB

    AndyB Well-Known Member

    There's no rand function for templates. Perhaps can use {$serverTime} instead.
     
    CyclingTribe likes this.
  3. CyclingTribe

    CyclingTribe Well-Known Member

    Thanks @AndyB - is {$serverTime} a Unix timestamp?
     
  4. Brogan

    Brogan XenForo Moderator Staff Member

    Use it like this:

    Code:
    <xen:set var="$var.8">
    ...
    </xen:set>
    
    {xen:raw '$var.{xen:calc '({$serverTime} % 8) + 1'}'}
    The 8 is the maxinum number you want to randomise between.
    Add a xen:set for each value.
     
    vfustar and CyclingTribe like this.
  5. CyclingTribe

    CyclingTribe Well-Known Member

    Thanks guys - I'll give that a try later - cracking support, as ever!! (y)
     
  6. CyclingTribe

    CyclingTribe Well-Known Member

    Okay, for whatever reason I cannot seem to get this working.

    However, I thought of perhaps a simpler way to randomise the insertions - just use the very last digit of $serverTime. It'll be 0 - 9 and over hundreds of thousands of requests should average out pretty well to around 10% each.

    Presumably I could do this in the template? If so, how would I get the last digit of $serverTime and display content based on its value?

    Thanks,
    Shaun :D
     
  7. Brogan

    Brogan XenForo Moderator Staff Member

    What couldn't you get working?

    Something like this should do it:
    Code:
    <xen:set var="$var.1">
    ...
    </xen:set>
    
    <xen:set var="$var.2">
    ...
    </xen:set>
    
    <xen:set var="$var.3">
    ...
    </xen:set>
    
    {xen:raw '$var.{xen:calc '({$serverTime} % 3) + 1'}'}
     
  8. CyclingTribe

    CyclingTribe Well-Known Member

    Well, I assumed that the content between the <xen:set var ... > and </xen:set> would be rendered directly onto the page, but that isn't happening. Is that how it should work?
     
  9. Brogan

    Brogan XenForo Moderator Staff Member

    That's how it was working for me.
    I was using exactly that for my featured threads until I turned it into an add-on.
     
    CyclingTribe likes this.
  10. cclaerhout

    cclaerhout Well-Known Member

    Code:
      <xen:if is="mt_rand(0,100) < 50">
         <xen:include template="custom_template_a" />
       <xen:else/>
         <xen:include template="custom_template_b" />
      </xen:if>
    
    Code:
      <xen:if is="mt_rand(0,100) < 50">
         <xen:set var="$myVariable">Value1</xen:set>
       <xen:else/>
         <xen:set var="$myVariable">Value2</xen:set>
       </xen:if>
       {$myVariable}
    
     
    CyclingTribe likes this.
  11. CyclingTribe

    CyclingTribe Well-Known Member

    Thanks Paul - unfortunately it wasn't working like that for me, however, after trying this:

    ... it seems to be doing the trick. (y)

    Thanks for your help gents - much appreciated.

    Cheers,
    Shaun :D
     

Share This Page