Skip to content

Bundles

Mark Croxton edited this page Oct 1, 2013 · 28 revisions

Bundles are categories for variables. There are three default bundles: default, template and static, and you are able to create your own by specifying a name with the bundle="" parameter when using the {exp:stash:bundle} tag.

If you are using the Multiple Site Manager, be aware that Bundles are 'global' objects and the same bundles can be referenced across sites. Variables assigned to bundles are site-specific.

If you are using Mustash, you can create and manage your bundles in the Control Panel.

###Built-in bundles

bundle="default"

This is the default place for all variables and lists that you create in your templates using the set/get syntax.

bundle="template"

This is the default bundle for all stash templates embedded with the {stash:embed} tag.

bundle="static"

This is the default bundle for all variables captured by the {exp:stash:static} tag.

Basic use

{exp:stash:set name="foo" bundle="my_custom_bundle"}bar{/exp:stash:set}

Saving form data to a custom bundle

When you need to persist form values across pages, the {exp:stash:bundle} tag can be used to save a group of variables as a single variable in a defined bundle. This is much more efficient when saving a large number of independent form variables.

{!-- set a static context for the form values, which we can reference as '@' --}
{exp:stash:context name="my_search_form"}

{!-- register form values, validate individually, and save to a custom 'form' bundle --}
{exp:stash:bundle name="form" context="@" refresh="10" parse="inward"}
    {exp:stash:get dynamic="yes" type="snippet" name="orderby" default="entry_date" match="#^[a-zA-Z0-9_-]+$#"}
    {exp:stash:get dynamic="yes" type="snippet" name="sort" default="asc" match="#^asc$|^desc$#"}

    {!-- multi-select fields get converted to a pipe-delimited string, e.g. 1|2|3 --}
    {exp:stash:get dynamic="yes" type="snippet" name="category" default="" match="#^[0-9|]+$#"}
{/exp:stash:bundle}

{!-- now we could use like this in an embedded view template --}
{!-- note that the use of '@' pointers means we can reuse the form code elsewhere --}
<input name="orderby" value="{@:orderby}">

{!-- if we have captured an array of values in a variable we can use {if IN()} --}
{exp:stash:get_bundle name="form" context="{segment_3}"}
<select name="category">
    {exp:channel:categories channel="entries" style="linear"}
        <option value="{category_id}" {if '{category_id}' IN ({@:category})} selected="selected"{/if}>{channel_title}</option>
    {/exp:channel:categories}
</select>
{/exp:stash:get_bundle}

Cache-breaking

Bundles can be a useful when you need to clear a group of cached variables that are related in some way.

Clone this wiki locally