Why use exp:stash:set in the view?
The set and get tags both have a parameter output=“yes|no”. This tells Stash to either output the variable or not. By default when using get, output=“yes”. When using set, output=“no” by default. In this case I’ve used set with output=“yes” because I want Stash to both set a variable if it does NOT exist and get a variable if it does. This is basically a ‘trick’ you can use for caching arbitrary bits of template code.
Replace parameter
This tells Stash whether it should replace a variable that already exists. By default replace=“yes” for both the get and set tags, meaning every time the template containg the tag is viewed, the Stash tag overwrites the variable value with it’s tagdata. If you set it to “no” then Stash will return the existing variable without replacing the variable’s value, or if the variable doesn’t exist, it will set it as normal. That means we can use it in combination with save=“yes” for persistent caching.
What is stash parsing? What is it exactly retrieving?
{!-- set/retrieve page_cache for 60 minutes --}
{exp:stash:set name="page_cache" save="yes" refresh="60" parse_tags="yes" replace="no" scope="site" context='@' output="yes"}
<div class= "latest_wrapper">
<div class="tab blog">
{exp:stash:get name="entries_blog" context='@'}
</div>
<div class="tab news">
{exp:stash:get name="entries_news" context='@'}
</div>
</div>
{/exp:stash:set}
When parse_tags=“yes” Stash will parse it’s tagdata (the text the tag pair encloses) using the EE template engine. It creates a new instance of the Template object and runs the tagdata through a single pass of it’s parser methods. You can choose to run multiple passes of the parser (for nested tags) by setting the parameter parse_depth=“X” where X is the number of passes desired (more passes = more processing/overhead). The rendered tagdata is then saved to the database using the variable name as it’s key. If you look in the Stash table in your database for the key you will see that the value stored looks something like:
<div class="tab blog">
blog: title 1
blog: title 2
</div>
<div class="tab news">
news: title 1
news: title 2
</div>
Clearing the cache
I AM planning on a control panel interface for managing the Stash cache in the future. This will likely be a paid add-on (the Stash core will always be free).
One thing I do is add this to the top of my viewModels (put it in a snippet):
{exp:switchee variable="get:flush" parse="inward"}
{case value="1"}
{exp:stash:flush_cache}
{/case}
{/exp:switchee}
Then when you visit the site in your browser append ?flush=1 to the url to clear the cache.