• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
  • This forum is for release discussion only. Discussions that do not relate specifically to the resource release should be discussed in another, more appropriate forum.
  • This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

Display A Random Banner

Jake Bunce

XenForo moderator
Staff member
#1
Here is some template programming to define and randomize your banners:

This code works in any template. For randomizing banner advertisements you are probably working in one of the ad_ templates. For example:

Admin CP -> Appearance -> Templates -> ad_above_content

Use this code:

Code:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:set var="$banners.1">banner one</xen:set>
<xen:set var="$banners.2">banner two</xen:set>
<xen:set var="$banners.3">banner three</xen:set>

{xen:raw '$banners.{xen:calc '({$serverTime} % 3) + 1'}'}
Replace the red parts with your banner code. Just be sure to use a new number for each new banner you add. And the highest number needs to be entered into the rand calculation. For example, if you have 5 banners defined then it will look like this:

Code:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:set var="$banners.1">banner one</xen:set>
<xen:set var="$banners.2">banner two</xen:set>
<xen:set var="$banners.3">banner three</xen:set>
<xen:set var="$banners.4">banner four</xen:set>
<xen:set var="$banners.5">banner five</xen:set>

{xen:raw '$banners.{xen:calc '({$serverTime} % 5) + 1'}'}
 

zaiger

Active member
#3
Hmm can't seem to get it to work. I am replacing the red part with <img src="banner url" />, correct? It is showing up at the top of the page as &lt;img src=&quot;http://forum.my-forum.com/static/banner.jpg /&gt;

Edit: Never mind i think I got it now :)
 

Ingenious

Well-known member
#4
This is excellent Jake and a good alternative to running a PHP file to do the same.

One question. Can you please advise in the original code above, how to change it so that only ONE banner from X is shown, randomly picked (rather than all of them, one after the other)?
 

Jake Bunce

XenForo moderator
Staff member
#5
Hmm can't seem to get it to work. I am replacing the red part with <img src="banner url" />, correct? It is showing up at the top of the page as &lt;img src=&quot;http://forum.my-forum.com/static/banner.jpg /&gt;

Edit: Never mind i think I got it now :)
Hmm, xen:set parses for htmlspecialchars. That sucks. I didn't think to check that in my testing.

Here is a different solution which doesn't have that problem:

Code:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:set var="$rand">{xen:calc '({$serverTime} % 3) + 1'}</xen:set>

<xen:if is="{$rand} == 1">
	banner one
</xen:if>

<xen:if is="{$rand} == 2">
	banner two
</xen:if>

<xen:if is="{$rand} == 3">
	banner three
</xen:if>
I have updated my original post.
 

Jake Bunce

XenForo moderator
Staff member
#6
This is excellent Jake and a good alternative to running a PHP file to do the same.

One question. Can you please advise in the original code above, how to change it so that only ONE banner from X is shown, randomly picked (rather than all of them, one after the other)?
It already does that. It shows only one of the banners you have defined.
 

zaiger

Active member
#9
Hmm, xen:set parses for htmlspecialchars. That sucks. I didn't think to check that in my testing.

Here is a different solution which doesn't have that problem:

Code:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:set var="$rand">{xen:calc '({$serverTime} % 3) + 1'}</xen:set>

<xen:if is="{$rand} == 1">
banner one
</xen:if>

<xen:if is="{$rand} == 2">
banner two
</xen:if>

<xen:if is="{$rand} == 3">
banner three
</xen:if>
I have updated my original post.
It worked, the problem was my fault. Didn't chmod the folder properly. I think I only had to make one small change to your original code:

Code:
<xen:set var="$banners.1">http://static.url.com/edf/banner.png</xen:set>
<xen:set var="$banners.2">http://static.url.com/edf/banner2.png</xen:set>
<xen:set var="$banners.3">http://static.url.com/edf/banner3.png</xen:set>

<xen:set var="$rand">{xen:calc '({$serverTime} % 3) + 1'}</xen:set>

<xen:foreach loop="$banners" value="$curBanner" i="$i" count="$count">
<xen:if is="{$i} == {$rand}">
<xen:set var="$randBanner">{$curBanner}</xen:set>
</xen:if>
</xen:foreach>

<img src="{$randBanner}" />
 

Jake Bunce

XenForo moderator
Staff member
#11
It worked, the problem was my fault. Didn't chmod the folder properly. I think I only had to make one small change to your original code:

Code:
<xen:set var="$banners.1">http://static.url.com/edf/banner.png</xen:set>
<xen:set var="$banners.2">http://static.url.com/edf/banner2.png</xen:set>
<xen:set var="$banners.3">http://static.url.com/edf/banner3.png</xen:set>

<xen:set var="$rand">{xen:calc '({$serverTime} % 3) + 1'}</xen:set>

<xen:foreach loop="$banners" value="$curBanner" i="$i" count="$count">
<xen:if is="{$i} == {$rand}">
<xen:set var="$randBanner">{$curBanner}</xen:set>
</xen:if>
</xen:foreach>

<img src="{$randBanner}" />
That old code works because you moved the HTML outside of xen:set. But if you ever need to set different HTML for each banner then you will have to use the new code.
 

Jake Bunce

XenForo moderator
Staff member
#12
It worked, the problem was my fault. Didn't chmod the folder properly. I think I only had to make one small change to your original code:

Code:
<xen:set var="$banners.1">http://static.url.com/edf/banner.png</xen:set>
<xen:set var="$banners.2">http://static.url.com/edf/banner2.png</xen:set>
<xen:set var="$banners.3">http://static.url.com/edf/banner3.png</xen:set>

<xen:set var="$rand">{xen:calc '({$serverTime} % 3) + 1'}</xen:set>

<xen:foreach loop="$banners" value="$curBanner" i="$i" count="$count">
<xen:if is="{$i} == {$rand}">
<xen:set var="$randBanner">{$curBanner}</xen:set>
</xen:if>
</xen:foreach>

<img src="{$randBanner}" />
Here is a simplified version of that old code if you want:

Code:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:set var="$banners.1">banner one</xen:set>
<xen:set var="$banners.2">banner two</xen:set>
<xen:set var="$banners.3">banner three</xen:set>

<xen:set var="$rand">{xen:calc '({$serverTime} % 3) + 1'}</xen:set>

{$banners.{$rand}}
Mike helped me with that syntax.
 

Cezz

Well-known member
#13
I might be wrong but I think you can put html in the xen:set but you then need to call it as {xen:raw $banners.{$rand}} so that it doesn't strip the HTML!

So in summary you could use!

HTML:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:set var="$banners.1"><img src="http://image1" /></xen:set>
<xen:set var="$banners.2"><script>adsense script</script></xen:set>
<xen:set var="$banners.3"><iframe src="http://advert3" width="798px" height="90px"></iframe></xen:set>
 
{xen:raw $banners.{xen:calc '({$serverTime} % 3) + 1'}}
 
F

Floris

Guest
#14
You're not wrong =D
Mike showed me earlier today to use container and then raw .. Nice tip Cezz.
 

Cezz

Well-known member
#15
You're not wrong =D
Mike showed me earlier today to use container and then raw .. Nice tip Cezz.
This is great news, and to me seems like a much better option than multiple xen:if blocks, I know if's are pretty cheap but still...

Maybe you would know (I know I could just test but meh) can you do a <xen:include template=""/> inside of a xen:set??? so could you set the variable to the template contents? Giving you the ability to then call that template multiple times latter on, say for example this code!

Code:
<xen:set "$extra"><xen:include template="extra_data_template" /></xen:set>

{xen:raw $extra}

<h1>Source Code</h1>
<pre>{$extra}</pre>
 
F

Floris

Guest
#16
Maybe I completely confuse the container option, but .. on the debug tool I updated today there is one template that hooks into the thread_view template setting a var based on the data I want, and the main template then spits out the raw stuff ...

<xen:container var="$xenfans_debug_threaddump">{xen:helper dump, $thread}</xen:container>

from the thread_view template, if I remember correctly, and then in the page_container template, i use ..

<textarea (removedextracode)>{xen:raw $xenfans_debug_forumdump}</textarea>

I end up with a textarea filled with the dump of the array.
 

Cezz

Well-known member
#17
Maybe I completely confuse the container option, but .. on the debug tool I updated today there is one template that hooks into the thread_view template setting a var based on the data I want, and the main template then spits out the raw stuff ...

<xen:container var="$xenfans_debug_threaddump">{xen:helper dump, $thread}</xen:container>

from the thread_view template, if I remember correctly, and then in the page_container template, i use ..

<textarea (removedextracode)>{xen:raw $xenfans_debug_forumdump}</textarea>

I end up with a textarea filled with the dump of the array.
No I think that is right, <xen:container> sets a variable that can be accessed from any of the templates, <xen:set> sets a variable that can be accessed by the current and child templates... or that is how I see it I may be wrong! I think <xen:set> can also be used within a <xen:include> to pass specific variables/code to the included template!
 

Jake Bunce

XenForo moderator
Staff member
#18
I might be wrong but I think you can put html in the xen:set but you then need to call it as {xen:raw $banners.{$rand}} so that it doesn't strip the HTML!

So in summary you could use!

HTML:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:set var="$banners.1"><img src="http://image1" /></xen:set>
<xen:set var="$banners.2"><script>adsense script</script></xen:set>
<xen:set var="$banners.3"><iframe src="http://advert3" width="798px" height="90px"></iframe></xen:set>
 
{xen:raw $banners.{xen:calc '({$serverTime} % 3) + 1'}}
xen:raw avoids the htmlspecialchars. Thank you!

But your syntax needs some single quotes in my testing. Here is new code using the $banners array that I originally used:

Code:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:set var="$banners.1">banner one</xen:set>
<xen:set var="$banners.2">banner two</xen:set>
<xen:set var="$banners.3">banner three</xen:set>

{xen:raw '$banners.{xen:calc '({$serverTime} % 3) + 1'}'}
I have updated my original post. Thanks guys.

Maybe you would know (I know I could just test but meh) can you do a <xen:include template=""/> inside of a xen:set??? so could you set the variable to the template contents? Giving you the ability to then call that template multiple times latter on, say for example this code!

Code:
<xen:set "$extra"><xen:include template="extra_data_template" /></xen:set>

{xen:raw $extra}

<h1>Source Code</h1>
<pre>{$extra}</pre>
Yes you can. I just tested it and it works.