19

I am using Stash only recently and was wondering how the performance is boosted over using embeds? I know there are a few less queries used in general, and I know you can subvert the parse order, but is there better reason to use it in templates?

I made a simple blog page with embeds and one with a layout partial and stash for the same exact content and there was little or no difference in {elapsed_time} - however for some reason there was one more query for the 'stashed' version.

The effort it is taking to think 'inside out' with stash, I am wondering what the big benefit it is over embeds with good caching?

This code would be included in an embed:

{exp:channel:entries channel="blog" limit="3"}
    <div class="recent-post">
       <a href="{site_url}blog/{url_title}" class="thumb">{exp:ce_img:single src="{blog_image}" height="54" width="54" crop="yes"}</a>
        <div class="post-head">
        <a href="{site_url}blog/{url_title}">{title}</a><span>{entry_date format="%d/%m/%Y"}</span>
        </div>
    </div>
{/exp:channel:entries}

Vs this 'stashed':

{exp:channel:entries channel="blog" limit="3"}
{exp:stash:append name="recent_blogs"}
<div class="recent-post">
    <a href="{site_url}blog/{url_title}" class="thumb">{exp:ce_img:single src="{blog_image}" height="54" width="54" crop="yes"}</a>
    <div class="post-head">
        <a href="{site_url}blog/{url_title}">{title}</a><span>{entry_date format="%d/%m/%Y"}</span>
    </div>
</div>
{/exp:stash:append}
{/exp:channel:entries}
{exp:stash:get name="recent_blogs"}
  • 2
    Excellent question. I am still struggling to "get" stash. – CreateSean Nov 15 '12 at 20:46
  • 1
    Did you run both sets of code in the embed template, or was the Stashed version run in the main/first template? If they were both run in the embed, they will be virtually the same performance. – Andrew Gunstone Nov 15 '12 at 23:26
  • 1
    Stash is still a struggle for me. Like @CreateSean I haven't had a lightbulb moment yet. – nonprofit_tech Nov 21 '12 at 8:07
  • Aaron, was there a correct answer to this question posted? – Anna_MediaGirl Dec 12 '12 at 5:13
17

Since the question is quite broad I'll try to summarise the advantages of the template partials pattern in general and Stash embeds in particular.

Template partials

  • allows code to be encapsulated into reusable chunks, to help you respect the DRY principle

  • allows markup (the 'view') to be separated from the code that retrieves and formats your data (the 'viewModel')

  • eliminates the need for multiple embedded templates and many common parse order problems that lead to degraded performance

As a consequence of the above the following becomes true:

  • code is more maintainable and less likely to break because it is separated into discrete concerns

  • the presentation layer for a single set of content can be swapped out easily, and can be done so on the fly using conditional logic. For example, a news story viewModel might have multiple views: HTML, RSS, JSON, print.

Stash embeds

Out of the box Stash embeds are configured to mimic EE embeds, and you won't see much of a performance gain. The real performance advantage comes from 'tuning' the embed parameters. With Stash embeds you can:

  • control the process stage - when in the parse order of the host template that the embed is included. This can be at the start of template processing, inline with the placement of the embed tag relative to other tags, or at the end of template parsing

  • control the parse stage - whether the template is parsed on set and cached, or cached then parsed on retrieval, or both

  • demarcate non-caching regions of the template with {stash:nocache}{/stash:nocache} tag pairs

  • precisely control the order of processing of embeds with the priority parameter

  • precisely control the parse depth when a template is parsed (the number of passes made by EEs template parser), and enable/disable conditional, variable and tag parsing per template

  • set caching duration per embed

  • use multiple instances of the same template without extra overhead

  • compile nested embeds into a single cached template

  • access native Stash variables in the the form {stash:my_variable}

13

You'll probably need to show more of your template code but where Stash could benefit you is that you could use a single channel entries tag to output both the single entry view data and the recent blog posts at the same time. Here's a good example from Mark Croxton which shows how he approaches a similar thing.

  • 2
    This is a great example, but it's really full on - using Stash embeds and 3 layers of separation. This is great for full DRYness but not sure the complexity outweighs the advantages. Unless it's a really large site I would opt for 2 layers of separation (an embedded view template and a combined data-fetching+markup template), and a little repeated markup for similar content types (eg, blog articles and press releases). – James Smith Nov 16 '12 at 9:11
10

It's hard to tell what you're trying to achieve with Stash in that code sample. Since you are both 'setting' and 'getting' in the same place you aren't benefiting from a separation of markup and data, and as you're not using any caching you won't see any performance benefits either. Here's a quick example that would add caching for subsequent requests:

{exp:stash:set
    name="recent_blogs"
    output="yes" {!-- Set and display the stash in one hit --}
    save="yes" {!-- Store the output in the DB --}
    parse_stage="set" {!-- Parse the content BEFORE caching it --}
    parse_tags="yes" {!-- We do want to parse module/plugin tags --}
    refresh="60" {!-- Our cache is valid for 60 minutes -- }
    replace="no" {!-- For subsequent requests inside the 60 minute window don't regenerate anything --}
    scope="site" {!-- This stash is valid for all users on all pages --}
}
{exp:channel:entries channel="blog" limit="3"}
    ...
{/exp:channel:entries}
{/exp:stash:set}

That said, template partials are what Stash does best. If you want an intro to the subject there are plenty of good articles about.

10

"I am using Stash only recently and was wondering how the performance is boosted over using embeds? I know there are a few less queries used in general, and I know you can subvert the parse order, but is there better reason to use it in templates?" Those were some of my own questions and with a couple of podcast from Lea Alcantara and Emily Lewis I got them solved:

Look for Adrienne Travis's http://ee-podcast.com/episodes/stash-template-partials and Mark Croxton's (Stash developer) http://ee-podcast.com/episodes/add-on-development-with-mark-croxton

  • I agree. The podcast made it 'click' for me. – Mutual Nov 18 '12 at 14:19
6

The performance gains are not just about using fewer embeds, they're equally about using fewer channel:entries tags, which are expensive. Your example is obviously not a complete web page, but if it were, how would you set the page's title and meta data and any other dynamic stuff that belongs in the <head>? How would you send related dynamic content into a sidebar area or the footer? How would you handle pages that need a slighty different layout? The native approach would be to pepper the templates with channel:entries tags to fetch and output data in the same breath.

Using Stash you can set those kind of things in a single channel:entries tag that also sets the body content, side content, footer content, extra javascripts or css, page options, and then spit them out again multiple times wherever you like in an embedded 'view' template or make conditional sections based on the stashed values.

The template partials approach also avoids the old 'dynamic sandwich' approach (ie, two embedded templates for your document header and footer)... Aside from the fact that one embedded template is better than two, it's also nice to have your entire 'view' markup contained in a single template.

2

Sorry to throw a cat amongst the pigeons but stash is very much a purist approach. Sure there are advantages of it but it takes a multi designed and structured site to a whole new level of complexity.

For me and now... Low Variables will do for the performance hit of embeds.

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.