4

It seems to me that using Stash Embeds with variables could be a really powerful way to keep things really dry.

I was wondering if anybody uses them to create consistent elements throughout their sites and just cache them away?

For example for a consistent call to action button throughout the site...

{exp:stash:embed:partials:cta stash:action="Download Now" stash:location="/download/"}

Which would embed the partial template which would look like

<a href="{exp:stash:location}" class="cta">{exp:stash:action} &raquo;</a>

It would allow for impressively DRY code which in turn would be consistent code (call to action buttons are things I notice become inconsistent quickly on a larger site) and would facilitate the ability to change hundreds of CTAs in one line of code.

Would this have much of a negative effect on the performance of the site? Has anybody tried something similar to this in the past?

1

Very late response here, and not quite an answer. In your case you want to cache all these things, in mine I want them to behave as functions, basically.

However it's in the same area so I thought might be useful for the discussion.

I use stash in a functional way all through he site I am developing. I guess coming from a programming background I just can't stand repeated code...so where I see it, I try and make a function.

Here's an example:

I 'call' it like this:


{stash:url_path}{stash:embed:utility:url_builder stash:channel_short_name="{channel_short_name}" stash:entry_id="{entry_id}" stash:urlt="{url_title}" random}{/stash:url_path}

and the 'function' itself is:


{!-- URL BUILDER --}
{!-- Takes: entry_id, channel_short_name, urlt (=url_title) --}
{!-- Takes: inks and paper_sub_cats - lists of categories --}
{!-- Returns: the URL stub for the entry e.g. /inkjet/print-packaging/cystral-clear-bags --}
{!-- Returns: 'ERROR' if it can't work it out--}

{!-- set a list of the inkjet categories--}
{snippet:global-global_set_inkjet_categories}   

{!-- get (one of..) the deepest category for matching in url_builder--}
{exp:gwcode_categories entry_id="{stash:entry_id}" last_only="yes" limit="1" parse="inward" style="linear"}
    {exp:stash:set name="deepest_cat_url_title" debug="yes" random}{cat_url_title}{/exp:stash:set}
{/exp:gwcode_categories}

{exp:switchee variable="stash:channel_short_name" parse="inward" debug="yes"}
        {case value="product_inkjet_paper"}/inkjet/paper/{stash:urlt}{/case}
        {case value="product_printer"}/inkjet/printer/{stash:urlt}{/case}
        {case value="product_monitor"}/monitors/{stash:urlt}{/case}
        {case value="product"}
            {!-- the tricky one - these products can be found at a number of different URLs --}
            {!-- so we need to use categories... --}    
            {switchee variable="stash:deepest_cat_url_title" parse="inward"}
                {case value="{stash:paper_sub_cats}"}/inkjet/paper/{stash:urlt}{/case}
                {case value="sample-packs"}/inkjet/sample-packs/{stash:urlt}{/case}
                {case value="print-finishing"}/inkjet/print-finishing/{stash:urlt}{/case}
                {case value="print-packaging"}/inkjet/print-packaging/{stash:urlt}{/case}
                {case value="albums-and-cards"}/inkjet/paper/{stash:urlt}{/case}
                {case value="{stash:inks}"}/inkjet/ink/{exp:stash:get name="deepest_cat_url_title" random}{/case}
                {case default="yes"}URL_BUILDER_ERROR{/case}
            {/switchee} 
        {/case}
{/exp:switchee}

...this works a treat to give me the full url to an entry. It does have one issue though - I get whitespace in the returned url. The url is perfectly functional, but I haven't worked out a way to trim the whitespace yet as stash embed don't have a trim parameter (have been meaning to ask about Mark Croxton about this).

I have several of these utilities that are key pieces of code. There is most likely a performance hit but it seems well worth if for the much better maintainability of isolating this logic to one place.

| improve this answer | |
  • Note the use of the random keyword when calling the utility - this prevents caching, and is required. – Jeremy Daalder Dec 13 '13 at 4:45
1

In stash builds, I would use stash embeds for things like sidebars, etc. so maybe not as granular as CTA buttons.

In those cases, I use them with the process=start parameter, which makes them act pretty much like EE snippets and include them in the other template before any other EE tags are parsed. Using that, I often don't need to pass variables around.

I also use parse_stage="set". Don't forget to turn of file syncing in production using stash_file_sync='false'.

| improve this answer | |

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.