Skip to content

Variables

Mark Croxton edited this page Mar 12, 2018 · 60 revisions

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 / get

Set and get a variable.

Standard syntax

{!-- 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}

Short syntax

{!-- set a variable --}
{exp:stash:foo}bar{/exp:stash:foo}

{!-- get a variable --}
{exp:stash:foo}

Append / prepend

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"}

Dynamic variables

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.

Default values

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}

Conditionals

If/else advanced conditionals

{!-- 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}

Switchee

{!-- 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}

Copy

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"}

Clone this wiki locally