• 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?

CTXMedia

Formerly CyclingTribe
#1
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
 

Brogan

XenForo moderator
Staff member
#4
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.
 

CTXMedia

Formerly CyclingTribe
#6
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
 

Brogan

XenForo moderator
Staff member
#7
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'}'}
 

CTXMedia

Formerly CyclingTribe
#8
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?
 

Brogan

XenForo moderator
Staff member
#9
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.
 

cclaerhout

Well-known member
#10
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}
 

CTXMedia

Formerly CyclingTribe
#11
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.
Thanks Paul - unfortunately it wasn't working like that for me, however, after trying this:

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>
... it seems to be doing the trick. (y)

Thanks for your help gents - much appreciated.

Cheers,
Shaun :D