XF 2.1 How to identify the last item of an array in a template foreach loop

Stuart Wright

Well-known member
I needed to create a comma separated list of items from an array in a template.
But not have a comma after the last item.
I did it this way. Hope it helps.

Code:
<xf:foreach loop="$array" value="$item" i="$i">
{$item}<xf:if is="{$i} < count($array)">,</xf:if>
</xf:foreach>

To identify the last item, $i == count($array)
 

Bespoke

Well-known member
Thank you for sharing. It is always nice to see how things are done and helps when searching these forums for answers.
Another way to convert the array to a comma separated string would be {{ $array|join(', ') }}
 

Stuart Wright

Well-known member
Thanks, except this is the actual code I have to use
Code:
<xf:foreach loop="$tags" value="$tag" i="$i"><xf:if is="{$tag.Tag.tag_name}">"{$tag.Tag.tag_name}"<xf:if is="{$i} < count($tags)">,</xf:if></xf:if></xf:foreach>
I am getting a specific value from the array and each element has to be in quotes.
Is there a better way of doing that?
 

Stuart Wright

Well-known member
And how I you get the first item from an array please?
I tried searching for 'template array' but nothing relevant came up.
Is there some documentation for everything you can do like this in templates?
I looked in the XF2 manual and there's nothing I could see.
Couldn't find anything relevant in resources after looking for a while.
Couldn't even get {xf:helper dump, $images} to work, taking a wild stab at what works in XF2.
I'd rather not pay someone to do something this simple.
Thanks
 

Lawrence

Well-known member
Here is the approach I would take, not sure if it is applicable to what you are trying to do (you do not need to remove the last , as CSS does it for the) list:

HTML:
    <xf:if contentcheck="true">
        <ul class="listInline listInline--selfInline listInline--comma">
                <xf:contentcheck>
                    <xf:foreach loop="$tags" value="$tag">
                        <xf:if is="$tag.Tag.tag_name">
                            <li>&#34;{$tag.Tag.tag_name}&#34;</li>
                        </xf:if>
                    </xf:foreach>
                </xf:contentcheck>
            </ul>
    </xf:if>

The &#34; is the HTML number for ".

XF 2 dump = {{ dump($images) }}
 
Last edited:

Lukas W.

Well-known member
And how I you get the first item from an array please?

{{ $array|first }}. To assign it to a variable, use set <xf:set var="$firstElement" value="{{ $array|first }}" />

I tried searching for 'template array' but nothing relevant came up.
Is there some documentation for everything you can do like this in templates?
I looked in the XF2 manual and there's nothing I could see.
Couldn't find anything relevant in resources after looking for a while.

Template filters/functions aren't documented I think. If you understand php code enough to read it, you can check XF\Template\Templater to see all template filters and functions available.

Couldn't even get {xf:helper dump, $images} to work, taking a wild stab at what works in XF2.
I'd rather not pay someone to do something this simple.
Thanks

Helpers have been deprecated, you use template functions now. To dump, it's either {{ dump($array) }} or if you want a simpler representation, you can use {{ dump_simple($array) }}.
 
Top