Work with stack, context and categories in ExpressionEngine

I hope there is a way to use the Stash Context with Categories, for example like this:

{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, but that would mean that someone would have to edit the template every 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 it even possible to dynamically use Stash contexts and ExpressionEngine categories?

+4
source share
3 answers

Any reason you need to use context? I guess I don’t understand how you are trying to use get_list to output data. Does something like this achieve what you need to do?

+4
source

The problem you are facing boils down to an exp: channel: entries loop doing many iterations, which then uses Stash to set up an associative array of values. When the channel: records is competing, you have an array of variables {this_category_name}, so it is never set as a regular variable.

So you really have a few options:

  • Use the URI segment as they are ranked earlier.
  • Use the exp: stash: set parameter to set the fragment type variable, which can be analyzed later on the page.
  • Hard code value

Here is my code that I tested and can confirm, works 100% at my end. 1 disclaimer, if your entries have several categories, this will break. I believe that this will use a set of the first category as part of the iteration.

Please note: do not forget to use the process = "end" parameters to control the parsing order, otherwise you will get 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 probably a much better way to solve your problem. The logic of what you are trying to do does not really add up. I was just trying to answer your questions about Stash, and if that were possible, that would be the best way.

+7
source

Maybe you need to wrap the whole append_list tag in {categories} {/ categories}, and not run it as a parameter of the stash tag?

However, this may be a problem with the parsing order, so you might need exp: stash: parsing there too.

+1
source

Source: https://habr.com/ru/post/1441667/


All Articles