[Guide] Explanation of XenForo arrays

Status
Not open for further replies.

Heretic121

OMG Member
Joined
Sep 24, 2010
Messages
446
Technically speaking they're not really "XenForo arrays" but it was just a simple way of saying "How to handle arrays in XenForo so that you can use them in your templates" which as you can probably tell is a little on the long side.

Right then, if you've read my topic which explains the function responseView (It can be found here if you haven't) then you'll know how to hand arrays of information over to XenForo's templates. This can be really handy but how do you use the arrays of information you give to the template?
Like this:
Code:
<xen:foreach loop="$info" value="$rand">
  {$rand.info} - {$rand.0}<br />
</xen:foreach>
This little code snippet has a lot of new things in that you probably haven't seen before so I'll explain each part.

<xen:foreach> - This is part XenForo's XSLT (http://en.wikipedia.org/wiki/XSLT). Simply, it gives you the ability to use a foreach function in your template, which is useful for cycling through arrays of information. The "loop" attribute is the array of information you want to cycle through and "value" is the attribute that says you want the value of it to be the name used for it in each cycle. This is basically a similar thing to PHP's foreach construct:
PHP:
foreach ( $info as $rand ) {
  echo "{$rand['info']} - {$rand[0]}<br />";
}
This would be the equivalent of the XSLT code above.

{$rand.info} / {$rand.0} - This is how you would refer to a variable in an array. Think of PHP once again, if you did:
PHP:
echo "$rand['info']";
It would fail, in some way, or at the very least not display how you would want it to. So you encase $rand['info'] with braces ( {} ) so that it would like this:
PHP:
echo "{$rand['info']}";
Which would cause it to display that perfectly fine. The same goes for when using arrays in XenForo's templates. There is only two differences between PHP's use of arrays and XenForo's. In PHP you use brackets around the "key" to value in the array, whereas in XenForo you need only put a fullstop after giving the variable name of the array (i.e. $rand) and then putting the key after it.
The other one is that in PHP you need to not only have brackets around string "keys" but you also need to have the string placed inside either single or double quotation marks, whereas in XenForo you can do exactly the same for both string "keys" and numerical "keys".

After explaining all this it'd be good to have an example to give you, so here it is:

This is what you do with your responseView
PHP:
$this->responseView('Test_ViewPublic_Index','test-template',array(
    'test' => array(
        array (
            'This is a string with a numerical key',
            'info' => 6  // This is a numerical value with a string key
        ),
        array (
            'This is a string with a numerical key',
            'info' => 7 // This is a numerical value with a string key
        )
    )
);
Then you can use it in your template like this:
Code:
<xen:foreach loop="$test" value="$info">
   {$rand.0} - {$rand.info}<br />
 </xen:foreach>
Which will end up returning this:
HTML:
This is a string with a numerical key - 6<br />
This is a string with a numerical key - 7<br />

So there you have it guys! That's how you use arrays in templates on XenForo. Hope you found this useful.

As always if you have any questions about this, let me know and I'll try to give you and answer to the best of my knowledge.
 

imported_Kier

XenForo Developer
Joined
Oct 6, 2010
Messages
3
Also useful in <xen:foreach> are the following attributes:
  • key
    The array key for the current value
  • value
    You already covered it - the current value
  • i
    The iteration count for the loop. Useful for doing things like displaying only the first 10 items from an array.
  • count
    The total number of items in the array
So a complete use of <xen:foreach> could look like this:
HTML:
<xen:foreach loop="$myArray" key="$arrayKey" value="$arrayValue" i="$iteration" count="$total">
...
</xen:foreach>
 

Heretic121

OMG Member
Joined
Sep 24, 2010
Messages
446
Thank you for the additional information Kier, it'll definitely be helpful :)
Also, thank you for liking my post, much appreciated :)
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,101
I had it on my list to poke Kier asking if he perhaps had any comments for a few of our guides. But he beat me to it himself! :) Thank you Kier for taking the time to educate us. Much appreciated.

Thank you Ben for reviewing the options in XenForo and publishing your findings in the form of guides. I see you've made 5 already! Awesome job.
 

Heretic121

OMG Member
Joined
Sep 24, 2010
Messages
446
Thank you Ben for reviewing the options in XenForo and publishing your findings in the form of guides. I see you've made 5 already! Awesome job.
You're most welcome Floris, I like, and enjoy, helping out the community. If you have any requests I'll have a look into making them, just let me know.
I have indeed! Thank you :) I plan to make more.
 

Steven Moore

OMG Member
Joined
Oct 22, 2010
Messages
1,084
I knew about the <xen:foreach> but had no idea you could use so many different functions in it. Thanks
 

Ajaxboy

OMG Member
Joined
May 2, 2011
Messages
8
How you translate:

if not - if(!$var) { } without using <xen:else />
 
Status
Not open for further replies.
Top