• 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

Zephyr

Well-known member
#1
Hello, how execute code once every two in a foreach please ?

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

Chris D

XenForo developer
Staff member
#3
The exact code for what you're trying to do is:
Code:
<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.
	</xen:if>
</xen:foreach>
 

ManOnDaMoon

Well-known member
#4
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 :
Code:
{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).