0

I'm not able to make sense of how to use context="" in stash.

I've not yet had to use it but I'm not sure if there are things I may have been able to handle more efficiently.

could somebody give an example please?

Thanks

1

Think of context as a prefix to the name parameter (that is in fact exactly how it works in the background). This enables you to use dynamic variables (or 'variable variables') that have the same name but might be used in different contexts. You don't have to use context - you could achieve the same thing with a dynamic name parameter e.g.: name="faqs-related-to-{entry_id}"

Personally, every site I build has a dedicated and permanent development/staging environment in addition to the live/production environment (and local environments per developer). So for me, context is an ideal way of scoping my Stash variables to the environment so that, for example, cached Stash lists are unique per environment. Without using context I'd have private draft status entries appearing on the live site due to the caching.

Here's an example of a cached list of 'news' entries where the staging environment's list can be different to the production environment's list thanks to context. Notice too that on the inside of the channel:entries loop I have another Stash list for the categories, which, instead of using context, achieves the same thing with the name parameter. (I prefer this as I always reserve the context parameter for environment scoping):

{exp:stash:set_list
    name="news-items"
    parse="yes"
    parse_depth="2"
    context="{global:env}"
    save="yes"
    replace="{global:stash_cache_replace}"
    scope="site"
    bundle="news"
}
    {exp:channel:entries
        channel="news"
        disable="member_data|pagination"
        dynamic="no"
        status="{global:entry_statuses}"
    }
        {stash:item-id}{entry_id}{/stash:item-id}
        {stash:item-title}{title}{/stash:item-title}
        {stash:item-date}{entry_date}{/stash:item-date}
        {stash:item-url}{if page_url}{page_url}{if:else}{url_title_path=news}{/if}{/stash:item-url}
        {stash:item-thumb}{news_img}{/stash:item-thumb}

        {exp:stash:set_list:2 name="cats-of-{entry_id}" context="{global:env}"}
            {categories}
                {stash:cat}{category_name}{/stash:cat}
                {stash:cat-url}{path=news}{/stash:cat-url}
            {/categories}
        {/exp:stash:set_list:2}

    {/exp:channel:entries}
{/exp:stash:set_list}

({global:env} is set in my config bootstrap and equates to "live" or "dev" depending on the environment, while {global:entry_statuses} equates to "open" or "open|draft" depending on the environment).

| 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.