4

I'm hoping there is a way to use Stash Context with Categories, for example, like so:

{exp:channel:entries channel="channel-name" dynamic="no" disable="member_data|pagination"}
    {exp:stash:append_list name='list' parse_tags="yes" save="yes" scope="site" context='{categories}{category_name}{/categories}'}
        {stash:this_title}{title}{/stash:this_title}
        {categories}
            {stash:this_category_name}
                {category_name}
            {/stash:this_category_name}
        {/categories}
    {/exp:stash:append_list}
{/exp:channel:entries}


{exp:stash:get_list name="list" parse_tags="yes" parse_conditionals="yes" context="{this_category_name}"}                   
    <div>
        all my stash variables and html etc.
    </div>
{/exp:stash:get_list}

I know I can do the following though, this would mean that someone would have to edit the template each time a category was added.

{exp:stash:get_list name="list" parse_tags="yes" parse_conditionals="yes" context="category1"}                  
    <div>
        all my stash variables and html etc.
    </div>
{/exp:stash:get_list}


{exp:stash:get_list name="list" parse_tags="yes" parse_conditionals="yes" context="category2"}                  
    <div>
        all my stash variables and html etc.
    </div>
{/exp:stash:get_list}

With that said, my question is: is there anyway to dynamically use Stash context and ExpressionEngine categories?

SQB
  • 3,926
  • 2
  • 28
  • 49
Natetronn
  • 466
  • 3
  • 12
  • Normally I set the context in the get_list via a segment though, this is on a single page for a jQuery accordion so not go there. Again, trying to avoid hardcoding the categories one by one for cases when a new category is added by client. Not sure it's possible or not. – Natetronn Oct 23 '12 at 22:56

3 Answers3

7

The problem you are having comes down to the exp:channel:entries loop running multiple iterations, which then uses Stash to set an associative array of values. Once the channel:entries is compete, you have an array of {this_category_name} variables, so it's never actually set like a regular variable.

So you really have a few options:

  1. Use a URI segment, since those are parsed early.
  2. Use the exp:stash:set tag to set a snippet type variable which can be parsed later on the page.
  3. Hardcode the values

Here is my code, which I have tested and can confirm works 100% on my end. 1 caveat, if your entries have multiple categories this will break. I believe this will use the first category set within the iteration.

Note, be sure to use the process="end" parameters to manipulate the parse order, otherwise you will end up with a blank screen.

{exp:channel:entries channel="your-channel" dynamic="no" disable="member_data|pagination"}
    {exp:stash:append_list name='list' parse_tags="yes" save="yes" scope="site" context='{categories}{category_name}{/categories}'}
        {stash:this_title}{title}{/stash:this_title}
        {categories}
            {stash:this_category_name}
                {category_name}
            {/stash:this_category_name}

            {exp:stash:set name="test_var" type="snippet"}{category_name}{/exp:stash:set}
        {/categories}
    {/exp:stash:append_list}
{/exp:channel:entries}

{exp:stash:parse process="end"}

    {exp:stash:get_list name="list" parse_tags="yes" parse_conditionals="yes" context="{test_var}" process="end"}
        <div>
            all my stash variables and html etc.
        </div>
    {/exp:stash:get_list}

{/exp:stash:parse}

I will add, that there is likely a much better way to tackle your problem. The logic of what you are trying to do doesn't really add up. I was merely attempting to answer your questions about Stash and if it could do it vs. being the best way.

Justin Kimbrell
  • 594
  • 4
  • 11
4

Any reason why you need to use context? I guess I'm not clear on how you are trying to use get_list to output the data. Would something like this achieve what you need to do?

Trevor Davis
  • 411
  • 3
  • 8
  • There may be a way to do it with match against though, I'm not sure. The point of context in get_list (and in append_list for that matter) is to allow me to loop through entries from certain categories. My second block of code works though, each category is hard coded meaning that if a new category is added then I will have to add said new category to the template as well ie. another get_list with said category's name in the context param. Which isn't a big deal though, I like hands off if at all possible. – Natetronn Oct 23 '12 at 23:06
  • Justin Kimbrell of couse was right on about everything he said though, it was the link from Trevor which ultimately lead the way to getting this done correctly and why I chose it as the right answer hoping it will help others in the future. I did however run into a possible bug with matrix tag pairs inside the categories tag pairs which you can read more about in my two replies here: http://devot-ee.com/add-ons/support/stash/viewthread/4021#25588 – Natetronn Oct 24 '12 at 06:59
1

Maybe you need to wrap your whole append_list tag in {categories}{/categories} rather than running it as a parameter of the stash tag?

That said, it could also be a parsing order issue so you might need exp:stash:parse in there somewhere too.

Tyssen
  • 1,569
  • 16
  • 35