1

It was my understanding that Stash embeds are cached by default. I'm working on an ecommerce site where all template groups have EE templates similar to:

{stash:embed
    name="file"
    context="@URI"
    file_name="views:file"
    process="static"
}

(I've got process=static in there but I've also got static cacheing turned off in my config for now. Will be looking at that part of things later.)

Each view file then contains code that looks like:

{stash:embed name="layouts:global"}
{exp:stash:set name="st_content"}Page content goes here{/exp:stash:set}

So I thought that I would need to escape the add to cart part of my product detail page with {stash:nocache}. But when I add that tag to my page, my form disappears and instead I get [_Stash_1].

If I remove the {stash:nocache} tags, the page appears as intended and then if I add products to the cart (which is also set up the same way and currently doesn't have any nocache tags in it), items get added to the cart successfully.

So my question is, am I misunderstanding how cacheing works with Stash or am I doing something else wrong and will I get tripped up somewhere if I didn't correct the cacheing set-up now?

3

You embed IS being "cached" - saved to the Stash database table as a variable - but it's not being parsed before being cached. That is because, out of the box, a Stash embed works just like an EE embed: the content of the embed file is included into the host template and then parsed at runtime.

The parsing stage of an embed is controlled by the parse_stage parameter.


parse_stage="set"

Parse the template the first time it is read from the file, and cache the rendered result. Subsequent retrievals will return the cached template from the database and not the original template file (unless you have set replace="yes" parameter or have set stash_file_sync = TRUE in your config).

parse_stage="get"

Read the template file and cache it to the database. When output to the template on the first and subsequent retrievals the template will be parsed. This is similar to how EE templates work.

parse_stage="both"

Parse the template before caching AND after it is retrieved. This can be very useful when enclosing regions of your template with {stash:nocache}{/stash:nocache}. On SET the template code inside {stash:nocache} will not be parsed, but everything else will. On GET it will be parsed. This provides a way to partially cache some of your template code while leaving other areas dynamic.


So to be clear: if you plan to cache a partially-rendered template with regions escaped with {stash:nocache}{/stash:nocache} tag pairs, then you will need to use parse_stage="both" on your main Stash embed.

Also please note that partial caching is incompatible with static caching, as a static-cached page does not pass through EE or even PHP at all - it's just served as plain old HTML. However, you can of course use javascript/ajax to update dynamic elements within a static-cached page.

| improve this answer | |
  • Excellent, thanks. Time to do some experiments with parse_stage=both and stash:nocache. – Tyssen Jan 22 '14 at 21:27

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.