1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. 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.
    Dismiss Notice
  3. 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

Discussion in 'Template Modifications [Archive]' started by Jake Bunce, Sep 8, 2011.

  1. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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'}'}
    
     
  2. zaiger

    zaiger Active Member

    This is great. Thank a lot!
     
  3. zaiger

    zaiger Active Member

    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 :)
     
  4. Ingenious

    Ingenious Well-Known Member

    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)?
     
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
    Brandon Sheley likes this.
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    It already does that. It shows only one of the banners you have defined.
     
    Ingenious likes this.
  7. Ingenious

    Ingenious Well-Known Member

    Thanks Jake.
     
  8. Brandon Sheley

    Brandon Sheley Well-Known Member

    good stuff, thanks for sharing :)
    I'll be using this shortly
     
  9. zaiger

    zaiger Active Member

    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}" />
    
     
  10. M@rc

    M@rc Well-Known Member

    Thanks for this.
    Should be helpful to me once I get everything ready on my new forum, :) .
     
  11. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
  12. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
    Brandon Sheley, Floris and zaiger like this.
  13. Cezz

    Cezz Well-Known Member

    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'}}
     
    Jake Bunce likes this.
  14. Floris

    Floris Guest

    You're not wrong =D
    Mike showed me earlier today to use container and then raw .. Nice tip Cezz.
     
    Cezz likes this.
  15. Cezz

    Cezz Well-Known Member

    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>
     
  16. Floris

    Floris Guest

    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.
     
  17. Cezz

    Cezz Well-Known Member

    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!
     
  18. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.

    Yes you can. I just tested it and it works.
     
    Brandon Sheley, Cezz and Floris like this.
  19. Floris

    Floris Guest

    It's almost like we're working together and learning something .. say it ain't so, SAY IT AIN'T SO !! ..
     
    Brandon Sheley, Cezz and Jake Bunce like this.
  20. ShadyX

    ShadyX Well-Known Member

    Ooo I like this. I could use it to display random logo images :D
     
    zaiger likes this.

Share This Page