croxton / Stash

Stash allows you to stash text and snippets of code for reuse throughout your templates.
GNU General Public License v3.0
197 stars 20 forks source link

save/cache exp:stash:set WITHOUT name, with wrapped names #34

Closed GDmac closed 11 years ago

GDmac commented 11 years ago

It seems that exp:stash:set WITHOUT name= (e.g. with wrapped stash pairs) doesn't honor save=yes and replace=no somehow. Is this intended behaviour? if so, is there a way to save the wrapped variables?

(edit: the following example fetches two entries from a channel, each one from a particular category. It first fetches one entry from category-one and then one entry from category-two. Because an entry can be assigned to multiple categories, it is possible that the second channel:entries tag fetches the same entry. To fetch two unique entries, the first entry_id is fed into the second channel:entries tag to exclude that entry_id)

{exp:stash:set 
  parse_tags="yes" parse_depth="4" parse_conditionals="yes" 
  save="yes" replace="no" scope="site"
  }
  // <!-- First get 1 entry from cat1, store the id and entry -->
  {exp:channel:entries channel="news" dynamic="no" limit="1" category="1"}
    {stash:news_cat1_id}{entry_id}{/stash:news_cat1_id}
    {stash:news_cat1_latest}{title}{/stash:news_cat1_latest}
  {/exp:channel:entries}
{/exp:stash:set}

{exp:stash:set 
  parse_tags="yes" parse_depth="4" parse_conditionals="yes" 
  save="yes" replace="no" scope="site"
  }
  // <!-- Fetch an entry from cat2, excluding first found entry_id by
  //       feeding it to chan:entries entry_id  -->
  {exp:channel:entries channel="news" dynamic="no" limit="1" category="2"
    entry_id="not {exp:stash:get name='news_cat1_id'}" parse="inward"
    }
    {stash:news_cat2_latest}{title}{/stash:news_cat2_latest}
  {/exp:channel:entries}
{/exp:stash:set}

// <!-- add the news to the content -->
{exp:stash:append name="content"}
  <div>
    {exp:stash:get name="slideshow"}
    {exp:stash:get name="news_cat1_latest"}
    {exp:stash:get name="news_cat2_latest"}
  </div>
{exp:stash:append}

(edit: stash:get fixed for example)

croxton commented 11 years ago

No, the variables are saved correctly and respect the replace="no" parameter. I think your problem is you are not accessing the variables correctly:

This is incorrect:

{exp:stash:get name="stash:news_cat1_latest"}

This is correct:

{exp:stash:get name="news_cat1_latest"}

{!-- short-tag syntax --}
{exp:stash:news_cat1_latest}

{!-- in nested template only, or within block parsed by Stash only --}
{stash:news_cat1_latest}
GDmac commented 11 years ago

ah, that was from copy-paste for re-creating the example. The problem is that when i turn template-debugging on, that channel:entries tags (or nested channel_images tags) still show up. e.g. it seems that the variables are not cached.

croxton commented 11 years ago

That's because the channel entries tag is outside the variable pairs that are captured. You can use a list instead of capturing multiple separate variables. This will behave like a single cached variable, in that the code wrapped by the list will not be subsequently run until the list cache expires.

{exp:stash:set_list name="my_list" parse_tags="yes" save="yes" replace="no" scope="site"}
  // <!-- First get 1 entry from cat1, store the id and entry -->
  {exp:channel:entries channel="news" dynamic="no" limit="1" category="1"}
    {stash:news_cat1_id}{entry_id}{/stash:news_cat1_id}
    {stash:news_cat1_latest}{title}{/stash:news_cat1_latest}
  {/exp:channel:entries}
{/exp:stash:set_list}

{exp:stash:get_list name="my_list"}
    {news_cat1_id}
    {news_cat1_latest}
{/exp:stash:get_list}