1

I've got an interesting problem that is leading me to believe I am approaching things totally wrong with stash... Essentially I am trying to call a "get_list" inside a variable that I am using as a placeholder in the layout.

Here is what I am trying to do:

I have a layout that I share across the site

<!DOCTYPE html>
{stash:embed:partials:global_html}
  <head>
    {stash:embed:partials:global_html_head}
  </head>
  <body class="{stash:body_class}">
    <div class='container shadow'>
      <header class='header-page' role='banner'>
        {stash:embed:partials:global_html_header}
      </header>
      <!-- /END #Header -->
      <section class='content' role='main'>
        {!-- Define the primary content or listing in the model. --}
        {if {exp:stash:not_empty name="content-primary"}}
          <article class='content-main'>
            {exp:stash:content-primary}
          </article>
          <!-- /END .content-main -->
        {/if}
      </section>
      <!-- /END section .content -->
      <section class='sidebar-page' role='complementary'>
        <h3>Sidebarpaloooooza</h3>
      </section>
      <!-- /END .sidebar-page -->
    </div>
    <!-- /END .container -->
    <footer class='footer-page container' role='contentinfo'>
      {stash:embed:partials:global_html_footer}
    </footer>
  </body>
  <!-- /END .body_class -->
</html>

In the layout I have placeholder variables to grab data from my models for display in the layout:

{!-- Define the primary content or listing in the model. --}
{if {exp:stash:not_empty name="content-primary"}}
  <section class='content-main'>
    {exp:stash:content-primary}
  </section>
  <!-- /END .content-main -->
{/if}
</section>
<!-- /END section .content -->

In my model I am grabbing a nested list of entries

{exp:stash:set_list
  name="listing"
  parse_tags="yes"
  parse_depth="2"
}
  {exp:channel:entries
    channel="ch_company_location"
    dynamic="no"
    {global:param_disable_default}
  }
      {stash:some_var}{cf_var_goes_here}{/stash:some_var}
      {!-- This is a Matrix Field --}
      {exp:stash:set_list:nested name="employees"}
        {cf_matrix_field}
          {stash:var_one}{cf_var_1}{/stash:var_one}
          {stash:var_two}{cf_var_2}{/stash:var_two}
          {stash:var_three}{cf_var_3}{/stash:var_three}
        {/cf_matrix_field}
      {/exp:stash:set_list:nested}
  {/exp:channel:entries}
{/exp:stash:set_list}

What I can't wrap my head around is how to get conditionals, {count}, {switch}, and {total_results} to work in when defining {exp:stash:content-primary}:

{!-- Primary content area --}
{exp:stash:set
  name="content-primary"
  output="no"
  parse_conditionals="yes"
  parse_tags="no"
  parse_vars="yes"
}
  {exp:stash:get_list
    name="listing"
    prefix ="parent"
  }
    {exp:stash:get_list:nested
      name="employees"
      prefix ="emp"
    }
      {if emp:count == 1}
        <div class="employees">
      {/if}
        <div class="employee {emp:switch="first||last"}">
          <dl>
            <dt>{var_one}</dt>
            <dd>{var_one}</dd>
            <dd>var_one</dd>
          </dl>
        </div>
      {if emp:count == emp:total_results}
        </div>
        <!-- /END .employees -->
      {/if}
    {/exp:stash:get_list:nested}
  {/exp:stash:get_list}
{/exp:stash:set}

No matter what I do I trigger the following errors:

Parse error: syntax error, unexpected ':' in /system/expressionengine/libraries/Functions.php(683) : eval()'d code on line 9

or

A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant count - assumed 'count'
Filename: libraries/Functions.php(683) : eval()'d code
Line Number: 9

I've tried parent:switch/parent:count/parent:total_results and just plain old {switch}/{count}/{total_results} in addition to the "emp:*" to no avail.

Am I approaching this incorrectly? Is there a way to define global "buckets" that I can load stash variables, lists, and nested lists into inside my layout(s)?

I'm using:

  • EE 2.5.5
  • Matrix 2.5.5
  • Stash 2.3.6
  • Doesn't parse_tags need to be yes in your {exp:stash:set name='content-primary'} ? – Jason Varga Jun 19 '13 at 2:10
  • I've tried it both ways with no luck but Mr. Croxton sent me a reply on twitter that I'll try out when I get to office here in a bit. – Paulo Elias Jun 19 '13 at 16:11
  • @JasonVarga I figured it out! I'll post he answer here shortly. – Paulo Elias Jun 19 '13 at 20:23
6

After some help from the wonderful Mr. Croxton I was able to sort it out. Here is how to pull this off.

When grabbing the data in your model you need to stash the {entry_id} of the parent list (I call mine {e_id} for no real reason). Then, when setting the nested list, provide context with the {entry_id} by using name="employees_{entry_id}".

{exp:stash:set_list
  name="listing"
  parse_tags="yes"
  parse_depth="2"
}
  {exp:channel:entries
    channel="ch_company_location"
    dynamic="no"
    {global:param_disable_default}
  }
      {stash:e_id}{entry_id}{/stash:e_id}
      {stash:some_var}{cf_var_goes_here}{/stash:some_var}
      {!-- This is a Matrix Field --}
      {exp:stash:set_list:nested name="employees_{entry_id}"}
        {cf_matrix_field}
          {stash:var_one}{cf_var_1}{/stash:var_one}
          {stash:var_two}{cf_var_2}{/stash:var_two}
          {stash:var_three}{cf_var_3}{/stash:var_three}
        {/cf_matrix_field}
      {/exp:stash:set_list:nested}
  {/exp:channel:entries}
{/exp:stash:set_list}

When getting the list you need to add a prefix to your lists (parent and nested) so that {count}, {switch}, and {total_results} work as intended. You also need to provide the nested list context using the stashed entry_id from the parent list. {e_id} in this case:

{!-- Set the primary content area --}
{exp:stash:set
  name="content-primary"
  output="no"
  parse_conditionals="no"
  parse_tags="no"
  parse_vars="yes"
}
  {exp:stash:get_list
    name="listing"
    prefix ="employees"
  }
    {if employees:count == 1}
      <div class="employees">
    {/if}
    {exp:stash:get_list:nested
      name="employees_{e_id}"
      prefix ="employee"
    }
      <div class="employee {employee:switch="first||last"}">
        <dl>
          <dt>{var_one}</dt>
          <dd>{var_two}</dd>
          <dd>{var_three}</dd>
        </dl>
      </div>
    {/exp:stash:get_list:nested}
    {if employees:count == employees:total_results}
      </div>
      <!-- /END .employees -->
    {/if}
  {/exp:stash:get_list}
{/exp:stash:set}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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