There is in fact a default=""
parameter available for Stash set, however it only comes into play if you define a match parameter to test the value being set against a regular expression:
Thus this will do what you want:
{exp:stash:set name="head_title" match="#^\S+#" default="{title}"}{news_meta_title}{/exp:stash:set}
One problem with this approach however is that if you pass a variable as a default value (e.g. {title}
) - and that value contains double quotes - that will blow up the parameters and throw a template parse error.
Switchee provides a simple way around this problem because you can pass a reference to the Stash variable to evaluate (rather than the variable value itself) by prefixing the name with stash:
{exp:stash:set name="head_title"}{news_meta_title}{/exp:stash:set}
{exp:switchee variable="stash:head_title" parse="inward"}
{case value=""}
{exp:stash:set name="head_title"}{title}{/exp:stash:set}
{/case}
{/exp:switchee}
Alternatively, blocks allow you to provide a fallback value when you get the variable - this can be useful if alternative markup is required when the default value is used:
For example:
{exp:stash:set name="head_title"}{news_meta_title}{/exp:stash:set}
{exp:stash:set name="entry_title"}{title}{/exp:stash:set}
{exp:stash:block name="head_title"}
{!-- fallback value if "head_title" is empty --}
{exp:stash:entry_title}
{/exp:stash:block}
default
parameter on the various set tags...