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?
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:
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With