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

Stuart Wright

Well-known member
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
 
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.
 
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?
 
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);
 
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:
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>
 
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.
 
I have to save integers, maybe pairs of ints and strings to one field.
Is there a change to work with them directly in the template?
Example:
"1,23,233" or
"1,cars,2,dogs,3,cats"

Somehow i need an array to say

<xen:foreach loop="$field" value="$value" key=$key>
This is the $key and the $value<br>
</xen:foreach>

I can save the data in any way, but i want to work with this value directly in the template.
What can i do, please?
 
Back
Top Bottom