0

I have two Stash embeds. Both embed templates accept a color variable, but it's optional:

{if '{stash:color}'}
  {stash:color}<br>
{/if}

If I explicitly set the variable in both embeds, everything works as expected:

{exp:stash:embed name='template1' stash:color='Red'}
{exp:stash:embed name='template2' stash:color='Green'}

Outputs:

Red
Green

If I set the variable in the first embed but not the second, it also works as expected:

{exp:stash:embed name='template1' stash:color='Red'}
{exp:stash:embed name='template2'}

Outputs:

Red

But if I leave the first variable empty and set the second, both are assigned the second value:

{exp:stash:embed name='template1'}
{exp:stash:embed name='template2' stash:color='Green'}

Outputs:

Green
Green

Is this a bug? Shouldn't Stash embed variables only be scoped to the embed template they're passed to?

  • Stash 2.6.0
  • EE 2.10.1

2 Answers 2

1

Stash embed variables are available anywhere in the template that is parsed later in the parse order, i.e. they create a variable with global scope. This is by design - so that, for example, child embeds inherit variables passed to their parent, and so that the embeds can pass information to siblings. I would argue this is a feature rather than a bug, but I understand why you may disagree.

When using multiple instances of the same embed that accepts variables, you should set each variable explicitly:

{exp:stash:embed name='template1' stash:color=''}
{exp:stash:embed name='template2' stash:color='Green'}
2
  • If that's the case, then what's the difference between {exp:stash:embed name='template1' stash:color='Red'} and {exp:stash:color}Red{/exp:stash:color}{exp:stash:embed name='template1'}?
    – kmgdev
    Mar 14, 2016 at 19:25
  • The difference is that embed variable values are assigned at the exact point the embed is parsed, which by default is at the end of template parsing (process="end" - but could be at the start, before the template is parsed if process="start"). So explicitly setting embed variables ensures that the resulting Stash variables have the desired value at the point the embed is parsed. Mar 16, 2016 at 9:23
0

It looks like you may need to include the file_name in your embed code.

The docs refer to the same template but it's likely that what you're seeing may be related.

Give it a shot and see what happens.

PS. How are you setting your stash var if they're both named the same ?

Your Answer

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

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