0

I'm just playing around with implementing Stash on a site, following the Template Partials Using Stash article, but now I seem to be having a scope/execution order issue.

Layout

<html>
 <head>...</head>
 <body>
  <div id="container">
   <div id="sidebar">
    {exp:member:custom_profile_data}
      {exp:stash:member_field_1}Foo{/exp:stash:member_field_1}
      {exp:stash:member_field_2}Bar{/exp:stash:member_field_2}
    {/exp:member:custom_profile_data}
   </div>
   <div id="content">
     {exp:stash:content}
   </div>
  </div>
 </body>
</html>

Content Partial

{embed="layouts/content"}
{exp:stash:content}
<div>
 <h1>{exp:stash:member_field_1}</h1>
 <h1>{exp:stash:member_field_2}</h1>
 ...
</div>
{/exp:stash:content}

I have some custom member fields that I need in a few places throughout my content sections, so I'm trying to retrieve them in the layout and assign them to variables, in the hopes that I could save my content from being littered with {exp:member] tags, but the variables don't seem to be available in my content partials.

I believe that it's not working because my Stash variables are being parsed before the values are assigned, and from what I believe I understand about the order of how EE is parsing the views, it goes something like this:

  1. Partial is retrieved
  2. Content is assigned to {exp:stash:content}
  3. Layout embed is retrieved
  4. Layout is parsed and {exp:stash:content} value injected.

Am I missing a feature/use-case of Stash, or am I simply using the wrong tool for this particular job?

Update: After racking my brain for days, and then writing this question, I tried saving the variable definitions in a snippet, which is called from my layout and it seems to work. I'd still be interested to know if there is a Stash implementation of this type of case, though.

1 Answer 1

2

You should try separating the data-fetching from the markup-assembly - this way you'll be able to cleanly fetch all your member variables and store them into Stash vars for use later (either in the same template or an embedded one). Here's a tutorial I wrote describing how to do this: http://www.jamessmith.co.uk/articles/expressionengine-stash-tutorial-level-2

1
  • Great articles. Now I'm off to re-write my templates :)
    – nageeb
    Mar 11, 2014 at 8:08

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.