2

I'm using Stash to set a value like so;

{exp:stash:set_value name="head_title" value="{title}"}

However, I have now added a new channel field {news_meta_title} and would like to pass this as the value of "head_title" if it has been entered, otherwise just use {title}.

I have tried doing this, but it didn't work;

{if news_meta_title}
    {exp:stash:set_value name="head_title" value="{news_meta_title}"}
{if:else}
    {exp:stash:set_value name="head_title" value="{title"}
{/if}

I was wondering if anyone had any pointers?

I know that it's all down to parse order, but not sure on how to resolve this.

1
  • 1
    We need to lobby Croxton for the addition of a default parameter on the various set tags... Jan 31, 2014 at 17:14

3 Answers 3

1

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}
3
  • Awesome, thanks so much Mark. I'll give this a try. Feb 4, 2014 at 10:49
  • Hey Mark, when I used the Switchee method it broke the page and ended up showing a lot of EE tags instead of content. When I used the Block method, this worked. however, when I then removed the meta title from the entry to test - the page still showed the meta title. Is this just to do with caching? Feb 4, 2014 at 12:17
  • Bingo. I tried the switchee method again after realising that because my use case required the switchee to be nested so I just needed to remove the 'exp:' from the opening and closing tags. Feb 4, 2014 at 12:23
1

I guess you could wrap that whole block with {exp:stash:parse process="end"} ... {/exp:stash:parse} to make it all parse at the end. Like this

{exp:stash:parse process="end"}
    {if news_meta_title}
        {exp:stash:set_value name="head_title" value="{news_meta_title}"}
    {if:else}
        {exp:stash:set_value name="head_title" value="{title"}
    {/if}
{/exp:stash:parse}

Mark's docs are here.

2
  • Thanks Scott. For some reason it just outputs the field name rather than the content of the field. :/ Jan 31, 2014 at 13:13
  • 1
    Ah, then perhaps it's time for some switchee magic. Jan 31, 2014 at 13:28
1

I think this might be due to using an advanced conditional, which I guess could result in parse order issues.

Maybe this would work:

{if news_meta_title}
    {exp:stash:set_value name="head_title" value="{news_meta_title}"}
{/if}
{if news_meta_title == ""}
    {exp:stash:set_value name="head_title" value="{title}"}
{/if}

If not, have you tried adding parse="inward" as Mark mentions in the documentation?

Alternatively, you could try this set-up instead, which works for me:

{exp:stash:set name="head_title" parse_conditionals="yes"}
    {if news_meta_title}
      {news_meta_title}
    {/if}
    {if news_meta_title == ""}
       {title}
    {/if}
{exp:stash:set}
4
  • Thanks Katrin, not sure why it won't work for me. It displays correct if there is a meta title entered, but if the field is empty then it won't show the title. Jan 31, 2014 at 15:07
  • 1
    What happens if you do this: {exp:stash:set name="head_title" parse_conditionals="yes"} {title} {exp:stash:set}? Does the title show then? Without knowing the rest of your template code and set-up, it's hard to pinpoint where the issue could lie.
    – Katrin
    Jan 31, 2014 at 15:27
  • 1
    edited above to close {title}. that might be your issue now. Jan 31, 2014 at 17:58
  • Well spotted Stephen!
    – Katrin
    Jan 31, 2014 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.