-
Notifications
You must be signed in to change notification settings - Fork 21
Variables
Stash allows you to capture text and snippets of code for reuse throughout your templates.
Variables can be set dynamically from the GET or POST superglobals and can optionally be cached for persistence across pages. You can even create snippets dynamically, and these will work just like EE’s snippets.
Variables that you create are available to templates embedded below the level at which you are using the tag, or later in the parse order of the current template.
Set and get a variable.
{!-- set a variable --}
{exp:stash:set name="foo"}bar{/exp:stash:set}
{!-- get a variable (prints "bar") --}
{exp:stash:get name="foo"}
{!-- get a variable into a block (prints "bar") --}
{exp:stash:block name="foo"}
If 'foo' doesn't exist, this will be printed
{/exp:stash:block}
{!-- single tag set --}
{exp:stash:set_value name="foo" value="bar"}
{!-- set multiple variables in one go --}
{exp:stash:set}
{stash:title}A title{/stash:title}
{stash:content}Some content{/stash:content}
{/exp:stash:set}
{!-- set a variable --}
{exp:stash:foo}bar{/exp:stash:foo}
{!-- get a variable --}
{exp:stash:foo}
Append or prepend to a variable. If the variable you are trying to append or prepend to doesn't exist, it will be created.
{exp:stash:set name="foo"}bar{/exp:stash:set}
{!-- append a value to an existing variable --}
{exp:stash:append name="foo"}foo{/exp:stash:append}
{!-- append another value using single tag --}
{exp:stash:append_value name="foo" value="foo"}
{!-- get a variable (prints "barfoofoo") --}
{exp:stash:get name="foo"}
Get a variable from the $_GET or $_POST superglobal arrays. Use match="#regex#" to validate the global value against a regular expression.
{!-- validate the value against a regular expression --}
{exp:stash:get name="foo" dynamic="yes" match="#^[a-zA-Z0-9]+$#"}
Warning: Never trust values passed in the superglobal arrays. ALWAYS take care to validate your variables.
Set a default value for your variable placeholders.
{exp:stash:get name="foo" default="bar"}
{exp:stash:block name="foo"}
If 'foo' doesn't exist, this will be printed
{/exp:stash:block}
{!-- Set a native Stash variable --}
{exp:stash:set_value name="foo" value="bar"}
{if {exp:stash:not_empty name="foo"}}
Do this
{/if}
{if "{exp:stash:get name='foo'}" == "bar"}
Do this
{/if}
{!-- Set a snippet variable --}
{exp:stash:set_value name="foo" type="snippet" value="bar"}
{if foo}
Do this
{/if}
{if foo == "bar"}
Do this
{/if}
{!-- Set a native Stash variable --}
{exp:stash:set_value name="foo" value="bar"}
{!-- Evaluate it --}
{exp:switchee variable="stash:foo" parse="inward"}
{case value="bar"}
Do this
{/case}
{/exp:switchee}
Make a copy of an existing variable. By default the cloned variable will be created with the same scope, context and type of the original, but that can be changed:
{exp:stash:set name="foo"}bar{/exp:stash:set}
{!-- make an identical copy --}
{exp:stash:copy name="foo" copy_name="foo_copy"}
{!-- copy the value but set an new context, type and scope for the copied variable --}
{exp:stash:copy
name="foo"
copy_name="foo_copy2"
copy_scope="site"
copy_context="foo"
copy_type="snippet"
}
{!-- prints "bar" --}
{exp:stash:get name="foo_copy"}
{!-- prints "bar" --}
{exp:stash:get name="foo_copy2" scope="site" context="foo" type="snippet"}
Getting started
Using Stash
Using Mustash
- Mustash
- Installing Mustash
- Managing variables
- Managing bundles
- Cache-breaking rules
- Mustash plugins
- Mustash Varnish plugin
- Mustash plugin development
- Mustash API
Template design patterns
Tag reference
- {exp:stash:set}
- {exp:stash:get}
- {exp:stash:block}
- {exp:stash:set_value}
- {exp:stash:append}
- {exp:stash:append_value}
- {exp:stash:prepend}
- {exp:stash:prepend_value}
- {exp:stash:copy}
- {exp:stash:context}
- {exp:stash:is_empty}
- {exp:stash:not_empty}
- {exp:stash:set_list}
- {exp:stash:get_list}
- {exp:stash:append_list}
- {exp:stash:prepend_list}
- {exp:stash:split_list}
- {exp:stash:join_lists}
- {exp:stash:list_count}
- {exp:stash:unset}
- {exp:stash:flush_cache}
- {exp:stash:bundle}
- {stash:embed}
- {exp:stash:extend}
- {exp:stash:parse}
- {exp:stash:cache}
- {exp:stash:static}
- {exp:stash:finish}
- {exp:stash:not_found}
- Short tag syntax
- Using Stash methods in your own add-ons