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

Looping in a template

Discussion in 'XenForo Questions and Support' started by bart, Mar 25, 2013.

  1. bart

    bart Active Member

    I found this post by Jake Bunch:

    <xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>
    <xen:if is="{$contentTemplate} == 'forum_list'">
        <xen:set var="$banners.1">banner one for index page</xen:set>
        <xen:set var="$banners.2">banner two for index page</xen:set>
        <xen:set var="$banners.3">banner three for index page</xen:set>
        <xen:set var="$banners.4">banner four for index page</xen:set>
    <xen:elseif is="{$contentTemplate} == 'thread_view'" />
        <xen:set var="$banners.1">banner one for thread page</xen:set>
        <xen:set var="$banners.2">banner two for thread page</xen:set>
        <xen:set var="$banners.3">banner three for thread page</xen:set>
        <xen:set var="$banners.4">banner four for thread page</xen:set>
        <xen:set var="$banners.5">banner five for thread page</xen:set>
    <xen:else />
        <xen:set var="$banners.1">banner one for all other pages</xen:set>
        <xen:set var="$banners.2">banner two for all other pages</xen:set>
        <xen:set var="$banners.3">banner three for all other pages</xen:set>
    <xen:foreach loop="$banners" value="$curBanner" i="$i" count="$count">
        <xen:if is="!{$rand}">
            <xen:set var="$rand">{xen:calc '({$serverTime} % {$count}) + 1'}</xen:set>
        <xen:if is="{$i} == {$rand}">
            {xen:raw '$banners.{$rand}'}
    And now I wonder if I can get to the value of the key. I would like to have these rows:

    <xen:set var="$banners.1">banner one for index page</xen:set>
    <xen:set var="$banners.22">banner two for index page</xen:set>
    <xen:set var="$banners.33">banner three for index page</xen:set>
    <xen:set var="$banners.444">banner four for index page</xen:set>

    And want to know the key. The 1, 22, 33 or 444 in this case. I need to have those two variables.

    Is that possible?
  2. bart

    bart Active Member

    Hm, I actually need three variables...
  3. Chris D

    Chris D XenForo Developer Staff Member

    <xen:foreach loop="$banners" key="$key" value="$curBanner" i="$i" count="$count">
    	{xen:helper dump, $key}
    That should dump the $key value for each loop.
    bart, Brogan and MattW like this.
  4. bart

    bart Active Member

    It does!!! Wow!
    Chris D likes this.
  5. bart

    bart Active Member

    Now this works:
        <xen:set var="$banners.32">Banner 1</xen:set>
        <xen:set var="$banners.39">Banner 6</xen:set>
        <xen:set var="$banners.54">Banner 82</xen:set>
        <xen:set var="$banners.74">No banner at all</xen:set>
        <xen:set var="$color.32">red</xen:set>
        <xen:set var="$color.39">blue</xen:set>
        <xen:set var="$color.54">green</xen:set>
        <xen:set var="$color.74">purple</xen:set>
    <xen:foreach loop="$banners" key="$key" value="$curBanner" i="$i" count="$count">
    <div>The key is {$key}</div>
    <div>The content is {$curBanner}</div>
    <div>The color is {xen:raw '$color.{$key}'}</div>
    Xenforo is absolutely briljant!
    Chris D likes this.

Share This Page