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

Execute code in a loop on two one a foreach

Discussion in 'XenForo Development Discussions' started by Zephyr, Nov 11, 2012.

  1. Zephyr

    Zephyr Well-Known Member

    Hello, how execute code once every two in a foreach please ?

    <xen:foreach loop="$renderedChildrenvalue="$child">
    xen:raw $child}
    // Here, execute code once every two (eg: 2, 4, 6, 8, 10, 12, etc.)
    And how test a variable inside a template please (ex: var_dump or echo) ?
    Thanks :)
  2. James

    James Well-Known Member

    The foreach has an extra parameter allowing you to specify a count, you need to use that and create an if statement.
    Chris D likes this.
  3. Chris D

    Chris D XenForo Developer Staff Member

    The exact code for what you're trying to do is:
    <xen:foreach loop="$renderedChildren" value="$child" i="$i">
    	{xen:raw $child}
    	<xen:if is="$i %2 == 0">
    		// Here, execute when iteration count is EVEN.
    	<xen:else />
    		// Optional. Here, execute when iteration count is ODD.
  4. ManOnDaMoon

    ManOnDaMoon Well-Known Member

    Hi Zephyr, the solution Chris submitted is the right one (N.B.: even=pair, odd=impair ;) ).
    In Chris' example, you can see how to test a variable with xen:if tags.

    If you want to dump a variable or a whole template param, XenForo has a template helper for that :
    {xen:helper dump, $thread}
    The above code inserted in a template will print the $thread variable into your page.
    More info : http://xenforo.com/community/threads/1-0-0-b1-finding-variables-available-to-templates.6071/ (I don't know if this one has an updated version in the Resource Manager, though).
    Chris D likes this.

Share This Page