Extension, Module

Archived
Forum
(read-only)

Stash

ExpressionEngine 2, ExpressionEngine 3, ExpressionEngine 4, ExpressionEngine 5, ExpressionEngine 6

Back to this add-on's main page
View Other Add-ons From Mark Croxton

     

Optimisation challenge: Dynamic lists with Stash

Support Request

JUK
JUK

I like to ask for help to optimise the code below and also solve one problem.

The task is to go through a list of musicians and store them so that we can create an index page with a heading for each letter of the alphabet for which entries exist.

This is the code that populates the lists (working):

{exp:stash:set_list name="list_artists" parse_tags="yes" parse_conditionals="yes" parse_depth="2"}
 {exp
:channel:entries
  channel
="music-artist"
  
orderby="title"
  
sort="asc"
  
limit="20"
  
dynamic="no"
  
disable="categories|category_fields|member_data|pagination"}
  
      {exp
:stash:append_list name="artists_{exp:trunchtml chars='1'}{title}{/exp:trunchtml}" match="#^[{exp:trunchtml chars='1'}{title}{/exp:trunchtml}]#" against="{title}" parse="inward"}
               {stash
:artist_name}{title}{/stash:artist_name}
      {
/exp:stash:append_list}
   
 {
/exp:channel:entries}
{
/exp:stash:set_list} 

1. Instead of calling the “trunchtml” extension twice, can’t I store the result in a Stash variable and call that instead?
But how? I couldn’t find a case where Stash variable is reused in the same loop.

2. I also tried to remove the outer Stash tag as I don’t see its need, but then I get no values. Why do I need it?

The above results can be easily output with eg.

<h4>Artists starting with "A":</h4>
{exp:stash:get_list name="artists_A"}
    
<p>{count}{artist_name}</p>
{/exp:stash:get_list} 

But here’s the problem I could not solve: How can I use just one loop to output all defined lists?

Thank you for your help!

Mark Croxton
# 1
Developer
Mark Croxton

I tend to use Loopee for things like that:

{!-- Capture artists --}
{exp
:stash:set_list name="list_artists" parse="yes"}
   {exp
:channel:entries
   channel
="music-artist"
   
orderby="title"
   
sort="asc"
   
dynamic="no"
   
disable="categories|category_fields|member_data|pagination"}
      {stash
:artist_name}{title}{/stash:artist_name}
   {
/exp:channel:entries}
{
/exp:stash:set_list}

{
!-- Output artists in A-Z order --}
{exp
:loopee foreach="A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z" parse="inward"}
   {exp
:stash:get_list name="list_artists" match="#^{loopee_value}#" against="artist_name" orderby="artist_name" sort="asc"}
      
<h1>{loopee_value}</h1>
      <
p>{artist_name}</p>
   
{/exp:stash:get_list} 
{
/exp:loopee} 

 

JUK
# 2
JUK

Hi Mark,

thanks for the pointer to Loopee. I’ll have a look.

I would be interested if it is possible to re-use a Stash variable in the same loop?

I found another way to retrieve the lists without Loopee:

{exp:stash:set_list name="alphabet"}
{stash
:letter}A{/stash:letter}
{stash
:letter}B{/stash:letter}
...
{stash:letter}Z{/stash:letter}
{
/exp:stash:set_list}

{exp
:stash:get_list name="alphabet"}
 
<h4>Artists starting with "{letter}":</h4>
 
{exp:stash:get_list:nested name="artists_{letter}" parse="inward"}
     
<p>{artist_name}</p>
 
{/exp:stash:get_list:nested}
{
/exp:stash:get_list} 

(This still needs some logic in case a list is empty.)

Mark Croxton
# 3
Developer
Mark Croxton

Yes it is. You could also do this:

{exp:stash:set_list name="list_artists" parse="yes" parse_depth="2"}
   {exp
:channel:entries
   channel
="music-artist"
   
orderby="title"
   
sort="asc"
   
dynamic="no"
   
disable="categories|category_fields|member_data|pagination"}
      {stash
:letter}{exp:trunchtml chars='1'}{title}{/exp:trunchtml}{/stash:letter}
      {stash
:artist_name}{title}{/stash:artist_name}
   {
/exp:channel:entries}
{
/exp:stash:set_list}

{exp
:stash:get_list name="list_artists" unique="letter" orderby="letter" sort="asc"}
 
<h4>Artists starting with "{letter}":</h4>
 
{exp:stash:get_list:nested name="list_artists" match="#^{letter}#" against="artist_name" orderby="artist_name" sort="asc" prefix="nested"}
     
<p>{nested:artist_name}</p>
 
{/exp:stash:get_list:nested}
{
/exp:stash:get_list} 
JUK
# 4
JUK

Great, thank you for that.

My thinking mistake was that I thought I could re-use a Stash variable inside the same loop. Looks like that is not possible, that’s why you did it in two.

Thanks!