1

I have a chain of embeds in Stash:

  • A "main" template that embeds {stash:embed name="foo:.index"}
  • foo:.index has 1 line of code: {stash:embed name="common:.serp" stash:group_id="6" stash:channel_name="members" stash:permalink_parameter="foo/details"}
  • common:.serp has something like this:
{exp:stash:set parse_tags="yes"}
   {stash:channel_entries}
      {exp:channel:entries channel="{stash:channel_name}" dynamic="no" group_id="{stash:group_id}" limit="10" orderby="date" paginate="bottom" sort="desc"}
       ...
       ...
       {stash:embed name="common:.avatar"}
  • In common:.avatar, I am trying to access the value of {member_images:total_rows}. However, it's just echoed as that tag and not the number I expect it to be. I have verified this field exists in the Channel fields (it's a Matrix field).

I'm a bit confused with EE's parse order. And I've tried putting process="start" where I embed common:.avatar hoping it would parse {member_images:total_rows}. No go. Any ideas what's wrong?

2
  • Do other custom fields work? If you replace the {stash:embed name="common:.avatar"} with the code it contains does that work? Commented Jan 10, 2013 at 17:45
  • I just tested. Right before the {stash:embed name="common:.avatar"}, I outputted {age}, which is a field in my Channel. It displayed the Channel data. I then put {age} as the first line in common:.avatar; that renders as the string "{age}". So everything OK in common:.serp, but not anymore in common:.avatar? Commented Jan 10, 2013 at 17:59

1 Answer 1

4

Ok, this is complex so please bear with me.

Unlike process="inline" and process="end", the process="start" parameter is relative to the processing stage of the host EE template, not the processing of the nested Stash template (which is undertaken by a separate instance of the template parser and occurs either in parallel with or after the EE parser has run). Logically therefore process="start" is impossible in a nested Stash embed, since the nested embed is included after the parent template, which is itself parsed inline with the host template parser.

Consequently, your embed {stash:embed name="common:.avatar"} is always going to be processed after the channel entries tag has run, since it is nested inside it and the EE parser processes a template from the outside inwards.

Stash embeds are saved into memory as variables with the same name as the embed. After embedding, they are accessible later on in your template parse order as ordinary Stash variables. Therefore one possible solution would be to embed the child template earlier (but not output it), and then access it as a variable.

{stash:embed name="common:.avatar" process="inline" output="no"}
{exp:stash:set parse_tags="yes"}
   {stash:channel_entries}
      {exp:channel:entries channel="{stash:channel_name}" dynamic="no" group_id="{stash:group_id}" limit="10" orderby="date" paginate="bottom" sort="desc"}
          {stash:common:.avatar}
          ...
       {/exp:channel:entries}
   {/stash:channel_entries}
{/exp:stash:set}

I've never tried this so I'm not entirely sure it will work, please confirm if it does.

Other than the above, I would like to point out that nesting embeds in this way is reintroducing some of the parse order problems that Stash is meant to solve. In addition to allowing code re-use (the DRY principle) Stash allows you to separate the 'data-setting' logic (the viewModel) from the presentation (view). You are mixing up these layers when really you should be trying to separate them - and it's coming back to bite you. It's better to think of your EE host template as the place you grab all the data that you will then 'push' to the embed(s) containing your markup. If you have common patterns of data setting in your host template then encapsulate them as snippets rather than embeds.

1
  • Oh man, this is tough. I'll have to study your response more. In my case, the {stash:embed name="common:.avatar ...} needs to be inside the {exp:channel:entries ...} as its actually trying to output stuff from the Channel data. I'll see about separating the data setting logic some more ... Commented Jan 10, 2013 at 19:05

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.