1

I'm having an issue with stash holding onto some content when looping through records.

I have a BrilliantRetail catalog listing page which loops through all the entries in a category. Within this tag, I'm using the related_products tag to pull out the related products and then store them in two different places within that loop so that I don't have to call and execute the related_products tag twice.

However, on products which don't have related entries it appears to be just repeating the stored content.

{exp:stash:set parse_tags="yes" parse_conditionals="yes" name="br_bulk"}
                        {exp:brilliant_retail:product_related product_id="{product_id}"}
                            {related_products}
                                {if "{related_type_id}"=="2"}
                                    <a href="{exp:brilliant_retail:path src='product/{related_url}'}" title="click here to view {related_title}">{exp:snippet total="40"}{related_title}{/exp:snippet}</a><br />
                                    </li>
                                 {/if}
                            {/related_products}
                        {/exp:brilliant_retail:product_related}
                {/exp:stash:set}

and then further down the page, I have two pieces of code which are:

 {if "{exp:stash:not_empty name='br_bulk'}"}
                                   <div class="bulk">                               
                                    <strong>Bulk Order Options Available</strong>
                                    <br /><br />
                                   {exp:stash:br_bulk}
                                   </div>
{/if}

followed by a final

{exp:stash:destroy name='br_bulk'}

Just before I close the {exp:brilliant_retail:catalog} tag.

However, it looks like this 'destroy' function isn't working as expected (or I've misunderstood its function).

  • I think the destroy tag is only used for clearing a cached variable (ie, a variable with save="yes" replace="no" and usually scope="site")... but that doesn't really answer your question. – James Smith Apr 30 '13 at 12:42
3

Given that the Stash set is inside a loop, the resulting output would repeat your code above multiple times. Thus you would have multiple instance of {exp:stash:not_empty name='br_bulk'} and {exp:stash:br_bulk} in your template. The first instance of each of these will be parsed by EE, but subsequent instances will be replaced with a marker that will later on be replaced with the cached value of the first instance. In other words, EE will cache the tag because it assumes the output will be identical.

To avoid this add the word random to your tags:

{if "{exp:stash:not_empty name='br_bulk' random}"}
...
{/if}

{exp:stash:br_bulk random}

Or, use context to make them unique, perhaps using the {entry_id} of the current iteration:

{exp:stash:set parse_tags="yes" parse_conditionals="yes" name="br_bulk" context="{entry_id}"}
   {exp:brilliant_retail:product_related product_id="{product_id}"}
      {related_products}
      {if "{related_type_id}"=="2"}
      <a href="{exp:brilliant_retail:path src='product/{related_url}'}" title="click here to view {related_title}">{exp:snippet total="40"}{related_title}{/exp:snippet}</a><br />
      {/if}
      {/related_products}
   {/exp:brilliant_retail:product_related}
{/exp:stash:set}

{if "{exp:stash:not_empty name='br_bulk' context='{entry_id}'}"}
   <div class="bulk">                               
      <strong>Bulk Order Options Available</strong>
      <br /><br />
      {exp:stash:get name="br_bulk" context="{entry_id}"}
    </div>
{/if}
| improve this answer | |
  • Awesome Job Mr Croxton :) Adding context fixed it (as you knew it would!). – madebyhippo Apr 30 '13 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.