View Full Version : random or flipping of section execution within template
libertate
06-02-2006, 05:16 PM
Currently I have two sections within a template which gets executed depending on the pageid.
<if condition="($pages['pageid'] ==...
... code A ...
<else />
... code B ...
</if>
How can I make code A and code B alternate, or randomly displayed, using only template script?
Thanks!
libertate
06-04-2006, 07:43 PM
bump, any ideas?
Adrian Schneider
06-04-2006, 08:35 PM
What are you trying to accomplish exactly? Will your template condition be executed more than once (in a loop), or just once? What would Code A and Code B contain?
If you want it random, have it generate a random number (say between 0 and 1), then in your template just check it. $num = rand(0, 1);<if condition="$num">Code A<else />Code B</if>
With a little more information I'll show you how to do what you want...
libertate
06-05-2006, 01:11 AM
I want the code to be kept within the template and not use external code.
I simply one to display two javascripts. It would not loop. Depending on a random state, it should display one or the other. It doesn't even have to be random, it could be ABABABABA i.e. flipping
Is there a template variable that changes at each load time? Time or or something else? Maybe then I can do a simple var div 2 != var / 2 to decide which section to execute.
Adrian Schneider
06-05-2006, 01:25 AM
If it is a one time per script thing, then I would do this:
<if condition="TIMENOW % 2">Code A<else />Code B</if>Otherwise I would do this:<if condition="exec_switch_bg() == 'alt1'">Code A<else />Code B</if>Every time my second example executes, it will alternate.
libertate
06-05-2006, 04:35 PM
Thank you!
Is there a round(), trunc(), ceiling(), floor() or similar function which I can use like this:
<if round("TIMENOW % 2")="TIMENOW % 2">Code A<else />Code B</if>
Or DIV (integer division)?
<if "TIMENOW DIV 2"="TIMENOW % 2">Code A<else />Code B</if>
Hellcat
06-05-2006, 05:01 PM
Try this:
<if condition="$myalternate == 1">
... code A ...
<if condition="$myalternate = 0"></if>
<else />
... code B ...
<if condition="$myalternate = 1"></if>
</if>
Note that there are only SINGLE equal signs ("... = ...") in the secondary IFs and the usual double ones ("... == ...") in the first one.
[EDIT]
This may not work on the first try, depending on how the single bits are assembled (all in one function call, or each in seperate function call), so may want to find a variable with global scope you can use/hijack.
For posts inside a thread, for example, you could use the $thread array ($thread[myalternate]) to keep your value between posts....
Adrian Schneider
06-06-2006, 02:16 AM
Why not just use plugins to accomplish most of the processing, so you can keep the templates nice and simple?
@Hellcat: You really shouldn't use PHP like that in templates. Though it works, it defeats the whole purpose of the template system. Someone editing it will most likely remove the empty conditions and break the script without knowing what happened to it.
@libertate: your syntax is a little off: <if condition="YOUR_CONDITION">
show this if YOUR_CONDITION is true
<else />
Show this if YOUR_CONDITION is false.
</if>
Note that the <else /> is optional.
Anyway, both of our examples do the same thing (alternate between true/false of the condition), so what exactly are you trying to accomplish here?
libertate
06-07-2006, 12:17 AM
Hah! It worked. Thank you all!
<if condition="TIMENOW % 2">Code A<else />Code B</if>The above works.
Coming from old school programming, I didn't realize that the "condition" was not a pseudonym, but the actual command, and the comparison was inside the quotes. :-/
So what does TIMENOW generate, and what does % mean? Is it MOD or something? Why is there no reference for this stuff (or so complicated to find / I looked on vB.com and couldn't find it)?
If you wanted to know...Code A is Google Adsense, Code B is Yahoo Publisher Network.
And it is showing positive impact to flip the advertisers periodically. :-D
Adrian Schneider
06-07-2006, 12:23 AM
TIMENOW is a Unix timestamp. % is mod, yes. The condition can basically be any PHP code, with the exception of only being able to run a few functions.
For more conditions you can use, you can just learn more PHP, but I think there are some basics in the vBulletin manual.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.