Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with Stash, Context and Categories in ExpressionEngine

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?

like image 865
Natetronn Avatar asked Dec 15 '22 18:12

Natetronn


2 Answers

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.

like image 134
Justin Kimbrell Avatar answered Dec 21 '22 22:12

Justin Kimbrell


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?

like image 27
Trevor Davis Avatar answered Dec 21 '22 23:12

Trevor Davis