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

XF 1.4 How do I add strings in a foreach loop?

Stuart Wright

Well-known member
#1
Folks I have the following code on a page which displays tags (for some content) over the top-left corner of an image.
E.g. https://www.avforums.com/review/panasonic-tx-50cx802b-cx802-cx800-4k-review.11261
Code:
<xen:foreach loop="$tags" value="$tag">
 <xen:if is="{$tag.tag_name}"><span><a href="{xen:link 'tag', $tag}">{$tag.tag_name}</a></span><br /></xen:if>
</xen:foreach>
I want to create an additional string variable which is a comma separated list of the tags.
How would I do this within the above loop please?
Thanks
 

SneakyDave

Well-known member
#2
Do you want to take your existing foreach loop, and create a comma separate list? That's what it sounds like... This will do that, but you'll end up with a comma after the last tag.
Code:
<span>
<xen:foreach loop="$tags" value="$tag">
<a href="{xen:link 'tag', $tag}">{$tag.tag_name}</a>,
</xen:foreach>
</span>
If you want to do it within the above foreach loop, then I'm not sure you can easily do it in a template, because explode() and in_array() functions aren't available to templates, IIRC.
 

Stuart Wright

Well-known member
#3
Thanks Dave.
What I need to do is build a string variable in the loop that I can then use outside of it.
E.g.
Code:
$taglist=""
<xen:foreach loop="$tags" value="$tag">
  <xen:if is="{$tag.tag_name}">$taglist .= {$tag.tag_name} . ","</xen:if>
</xen:foreach>
and then, as you say, truncate the ending comma of $taglist so I can then
Code:
<meta property="tags" content="{$taglist}">
Possible?
 

SneakyDave

Well-known member
#4
I don't think it is within a template. You'd need to probably need access to the implode() function, which I don't think you can use in templates.

It would look something like this, if it was supported in templates.
Code:
$comma_delimited_tags = implode(",",$tags);
 

Chris D

XenForo developer
Staff member
#5
This is untested:
Code:
<xen:set var="$tagList" value="" />
<xen:foreach loop="$tags" value="$tag">
    <xen:if is="{$tag.tag_name}">
        <xen:set var="$tagList" value="{$tagList}, {$tag.tag_name}" />
    </xen:if>
</xen:foreach>
 
Last edited:

Chris D

XenForo developer
Staff member
#6
Incidentally, you can use implode in templates. Search templates to see the syntax of that. Though you would need an array of tag names rather than an array of tag arrays which it seems you have now.
 

Chris D

XenForo developer
Staff member
#7
I've actually had a chance to test this properly, now. It works fine, however there's a small change which removes superfluous commas:

Code:
<xen:set var="$tagList" value="" />

<xen:foreach loop="$tags" value="$tag">
    <xen:if is="{$tag.tag_name}">
        <xen:set var="$tagList" value="{xen:if '{$tagList}', '{$tagList}, '}{$tag.tag_name}" />
    </xen:if>
</xen:foreach>
 

Stuart Wright

Well-known member
#8
I've actually had a chance to test this properly, now. It works fine, however there's a small change which removes superfluous commas:
Thank you, Chris, that's brilliant.
And for anyone wanting to see, it's working in this page:
https://www.avforums.com/review/panasonic-tx-50cx802b-cx802-cx800-4k-review.11261
View source and search for
Code:
<meta property="keywords"
And is showing with the structured data (rich snippet) being validated here:
https://developers.google.com/struc...sonic-tx-50cx802b-cx802-cx800-4k-review.11261
Hopefully this will get Google to present our reviews in a more useful way in its search results.