1

I have something like this:

{exp:stash:set name="name"}John{/exp:stash:set}     
{stash:embed name="message"}

{exp:stash:set name="name"}Jane{/exp:stash:set}     
{stash:embed name="message"}

In the first embed of "message", I want to display "John". In the second embed of "message", I want to display "Jane". Right now, I seeing "Jane" in both rendering of "message". Is there a way to do what I am trying to do?

2 Answers 2

2

Denver is right that embeds get processed at the end of template parsing by default, therefore the value of the "name" variable will always have the last value. What you could do is use the parameter process="start" so that the embed code is included before template parsing starts.

{exp:stash:set name="name"}John{/exp:stash:set}     
{stash:embed name="message" process="start"}

{exp:stash:set name="name"}Jane{/exp:stash:set}     
{stash:embed name="message" process="start" random}

Note that you would need to access your "name" variable as a tag in the embed, and that this tag should not be nested otherwise due to the way EE parses from outwards inwards, the variable would be changed to the later value.

{exp:stash:get name="name"}

If you do need the variables to coexist and not overwrite each other regardless of how you nest them in the embed, you could use a dynamic context to provide a namespace and refer to it with the @ pointer from within the embed itself:

{exp:stash:set name="name" context="message_1"}John{/exp:stash:set}     
{stash:embed name="message" stash:the_context="message_1"}

{exp:stash:set name="name" context="message_2"}Jane{/exp:stash:set}     
{stash:embed name="message" stash:the_context="message_2"}

In the embed:

{exp:stash:context name="{stash:the_context}"}

{exp:stash:get name="@:name"}
1
  • Another approach would be to pass the values directly to the stash embed as parameters in the form stash:var="Jane" (or replace "Jane" with a stash:get tag and add parse="inward" to tell EE to parse the parameter). That might be easier if you have just a few vars but if you have a whole bunch then using a context is more elegant/readable. Jan 2, 2013 at 12:49
0

Try {stash:embed process="inline" name="message"}. Default setting is end which tells stash to parse those values at the end of the parse order.

Passing a variable to the template is also possible this way: {stash:embed name="foo:bar" process="start" stash:my_var="value"}

Source: https://github.com/croxton/Stash

5
  • Sorry. I'm not seeing where it says that embed is deprecated. It works fine for me. I'm trying to set a variable that I want available in the embedded stash. Jan 2, 2013 at 8:36
  • My bad, the file-parameter is deprecated. Sorry for that. Anywway, try adding the process-parameter as the default setting is 'end' which tells stash to parse the values at the end. This is probably why your first value gets overwritten by the second one before you are printing this value.
    – Denver
    Jan 2, 2013 at 8:38
  • Nope. Now, instead of both showing "Jane", they both show "John" now. So, the opposite is happening. Jan 2, 2013 at 9:21
  • Please check the process and parse_stage parameters at github.com/croxton/Stash. They should solve your problem imho.
    – Denver
    Jan 2, 2013 at 12:31
  • Pretty sure Denvers solution would also work, but EE is caching the first instance of the embed and using it for the second. Add the word 'random' to the second embed tag to stop EE using the cached instance. Jan 2, 2013 at 13:16

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.