1. 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?

Discussion in 'Styling and Customization Questions' started by Stuart Wright, Jun 9, 2015.

  1. Stuart Wright

    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
     
  2. SneakyDave

    SneakyDave Well-Known Member

    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.
     
  3. Stuart Wright

    Stuart Wright Well-Known Member

    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 likes this.
  4. SneakyDave

    SneakyDave Well-Known Member

    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);
    
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    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: Jun 10, 2015
    SneakyDave and Jeremy like this.
  6. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
    SneakyDave and Jeremy like this.
  7. Chris D

    Chris D XenForo Developer Staff Member

    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>
     
    SneakyDave, Steve F and Bob like this.
  8. Stuart Wright

    Stuart Wright Well-Known Member

    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.
     
    SneakyDave likes this.

Share This Page