Skip to content

Instantly share code, notes, and snippets.

@croxton
Last active April 8, 2018 02:24
Show Gist options
  • Save croxton/0b209a91866018217ddb to your computer and use it in GitHub Desktop.
Save croxton/0b209a91866018217ddb to your computer and use it in GitHub Desktop.
Stash embeds as snippets

Stash embeds with the process="start" parameter can be used to include pre-parsed or unparsed template tags in your template before any other tags or variables are parsed. This can be a more flexible alternative to EE's built-in snippet functionality.

It makes sense to combine snippets that are used globally into a single file so that they are included with the minimum of overhead. Call it somewhere in your template:

{!-- template at /stash_templates/snip/global.html --}
{stash:embed:snip:global process="start"}

Inside the stash embed:

{!-- unparsed snippets --}    
{exp:stash:snip_1 type="snippet"}
   <h1>Snippet 1</h1>
{/exp:stash:snip_1}

{exp:stash:snip_2 type="snippet"}
   <h1>Snippet 2</h1>
{/exp:stash:snip_2}

{!-- pre-parsed and cached snippets --}    
{exp:stash:snip_3 type="snippet" parse_tags="yes" save="yes" scope="site" replace="no"}
     <ul class="latest-news">
     {exp:channel:entries dynamic="no" channel="blog" limit="5"}
        <li>{title}</li>
     {/exp:channel:entries}
     </ul>
{/exp:stash:snip_3}

The variables {snip_1}, {snip_2} and {snip_3} can now be used in your template exactly like snippets.

@bossanova808
Copy link

Ok - that makes sense & thanks. I guess ultimately every site has a fair amount of global stuff (header/nav/search/footer etc), so that makes real sense. I guess other pages, e.g. different types of product pages, each also use a (sub)set of blocks so one could break it up at the top of each template with just a couple of embeds each of which contains the major blocks needed for that layout.

{stash:embed:snip:global process="start"} {!-- contains all the global blocks --}
{stash:embed:snip:products process="start"} {! all blocks for various type of product pages say --}

So conceptually I guess this seems to fit best with the layout template? So in _stash/layouts/product_complex for example - have these up at the top (I find it hard to know where to put things because stash is so flexible!!)...then the layout just uses {head}, {navigation} etc - much cleaner syntax and only one or minimal embeds. And these blocks are then filled up with data from models etc.

The alternative would be the main template, which is where I put my models/and or direct tags if it's something simple. But that seems a bit less logical (and more repetitive as I only have a few layouts but lots of templates...

Any thoughts on best structure? Are their caching implications (I've yet to get to that bit!).
And I presume two embeds would be better rather than one giant embed with a whole host of blocks eg? It's an instance of the parser vs. a large amount of data/memory I guess?

Thanks for stash, don't know what I'd do without it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment