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

     

Nesting tags and performance

Support Request

Daniel
Daniel

Hey Mark,

I was wondering if Stash can do something similar to what low describes in this blog post, but in a cleaner way and if possible without enabling PHP in the template?

Mark Croxton
# 1
Developer
Mark Croxton

Absolutely!

{exp:channel:entries channel="news" dynamic="no" disable="member_data|pagination|category_fields"}
 {categories}
  {exp
:stash:append name="news_{category_id}"}
   
<h3>{title}</h3>
   
{news_body}
  {
/exp:stash:append}
 {
/categories}
{
/exp:channel:entries}

{exp
:channel:categories channel="news" style="linear" disable="category_fields" show_empty="no"}
 
<h2>{category_name}</h2>
 
{exp:stash:get name="news_{category_id}"}
{
/exp:channel:categories} 
Daniel
# 2
Daniel

Mark i’m using this technique but whenever I put a conditional inside the append tag I just seem to get a list of {categories}{/categories} tags.

I’m sure I have to use the parse_conditionals=“yes” parameter but it doesn’t seem to make any difference.

{exp:stash:set name="content" parse="inward" output="no" parse_tags="yes"}
 {exp
:channel:entries channel="{channel}" dynamic="no" disable="member_data|pagination|category_fields" status="not closed|draft" orderby="title" sort="asc" parse="inward"}
  {categories}
   {exp
:stash:append name="club_{category_id}" parse="inward" parse_conditionals="yes" parse_depth="2"}
    {if cf_club_phone_number}
<p>Phone</dt> <dd>{cf_club_phone_number}</p>{/if}
   {
/exp:stash:append}
  {
/categories}
 {
/exp:channel:entries}

 {exp
:channel:categories channel="{channel}" style="linear" disable="category_fields" show_empty="no"}
  
<section class="club-category">
   <
header>
   <
h1>{category_name}</h1>
   <
div><p>{category_description}</p></div>
  </
header>
   
{exp:stash:get name="club_{category_id}"}
  
</section>
 
{/exp:channel:categories} 

Sure this is blatantly obvious but I cant see the wood from the trees at the moment.

Mark Croxton
# 3
Developer
Mark Croxton

It’s because your conditional is inside the special {categories} tag pair. I think you’ll find even if you weren’t stashing it then that won’t work. I encountered a very similar thing the other day and worked out a way around it. When I get into the office I’ll try to find it and post it here.

Mark Croxton
# 4
Developer
Mark Croxton

If you have only one category assigned per entry:

{exp:channel:entries channel="news" dynamic="no" disable="member_data|pagination|category_fields"}
  {exp
:stash:append name="code_{categories limit='1'}{category_id}{/categories}" parse="inward"}
   
<h3>{title}</h3>
   
{news_body}
  {
/exp:stash:append}
{
/exp:channel:entries}

{exp
:channel:categories channel="news" style="linear" disable="category_fields" show_empty="no"}
 
<h2>{category_name}</h2>
 
{exp:stash:get name="news_{category_id}"}
{
/exp:channel:categories} 

If not you can use a list and use conditionals when getting the list:

{exp:channel:entries channel="news" dynamic="no" disable="member_data|pagination|category_fields"}
 {categories}
  {exp
:stash:append_list name="news_{category_id}"}
   {stash
:item_title}{title}{/stash:item_title}
   {stash
:item_body}{news_body}{/stash:item_body}
  {
/exp:stash:append_list}
 {
/categories}
{
/exp:channel:entries}

{exp
:channel:categories channel="news" style="linear" disable="category_fields" show_empty="no"}
 
<h2>{category_name}</h2>
 
{exp:stash:get_list name="news_{category_id}"}
 {if item_title}{item_title}{
/if}
 {
/exp:stash:get_list}
{
/exp:channel:categories} 
Daniel
# 5
Daniel

You sir are a legend.

Daniel
# 6
Daniel

Mark sorry to bother you once again but what do I need to do to pull data out, into a list item, that is stored in a matrix field?

Eg:

{stash:item_strapline}{cf_product_strapline}{/stash:item_strapline}
{stash
:item_stars}
 {cf_product_attributes}{mtrx_attribute_rating}{
/cf_product_attributes}
{
/stash:item_stars} 

Item stars contains the matrix field tag pair.

Cheers
Dan

Mark Croxton
# 7
Developer
Mark Croxton

Hi Daniel

if you wanted to use a list you would need to set the values inside the matrix tag pair. don’t put the stash:append_list inside a stash:set though!

{exp:channel:entries limit="1" ...}

{
!-- set stash string variables --}
{exp
:stash:set}
{stash
:item_strapline}{cf_product_strapline}{/stash:item_strapline}
...etc...
{/exp:stash:set}

{
!-- set a stash list variable --}
{cf_product_attributes}
{exp
:stash:append_list name="item_stars"}
{stash
:rating}{mtrx_attribute_rating}{/stash:rating}
{
/exp:stash:append_list}
{
/cf_product_attributes}

{
/exp:channel:entries} 

 

Daniel
# 8
Daniel

Crap I see Devotee chopped off most of my code block from the above message, which showed that this was all contained within a stash_list already.

So I’m using the category/list technique you so eloquently came up with above.

{exp:channel:entries channel="news" dynamic="no" disable="member_data|pagination|category_fields"}
 {categories}
  {exp
:stash:append_list name="news_{category_id}"}
   {stash
:item_title}{title}{/stash:item_title}
   {stash
:item_body}{news_body}{/stash:item_body}

    {stash
:item_stars}
        {cf_product_attributes}{mtrx_attribute_rating}{
/cf_product_attributes}
    {
/stash:item_stars}

  {
/exp:stash:append_list}
 {
/categories}
{
/exp:channel:entries}

{exp
:channel:categories channel="news" style="linear" disable="category_fields" show_empty="no"}
 
<h2>{category_name}</h2>
 
{exp:stash:get_list name="news_{category_id}"}
 {if item_title}{item_title}{
/if}
 {
/exp:stash:get_list}
{
/exp:channel:categories} 

And if I understand correctly there is no way to append a list into another list is there?

Sorry for the confusion I’ll make sure I preview before posting in the future to make sure all the code blocks are complete.

Thanks so much.
Dan

Mark Croxton
# 9
Developer
Mark Croxton

Hi Dan

you can append to a list but that adds another index at the root level of the multidimensional array, it doesn’t add to one of the nested arrays of key > value pairs.

In your case you might do something like:

{exp:channel:entries channel="news" dynamic="no" disable="member_data|pagination|category_fields"}
 {categories}
  {exp
:stash:append_list name="news_{category_id}" parse_tags="yes"}
   {stash
:item_title}{title}{/stash:item_title}
   {stash
:item_body}{news_body}{/stash:item_body}

    {stash
:item_stars}
        
<ul>
        
{cf_product_attributes}<li>{mtrx_attribute_rating}</li>{/cf_product_attributes}
        
</ul>
    
{/stash:item_stars}

  {
/exp:stash:append_list}
 {
/categories}
{
/exp:channel:entries} 

You’ll need the latest Stash 2.1.0 for parse_tags to work on the append_list tag. Also, I’d make the content inside {stash:item_stars} a snippet.

Natetronn
# 10
Natetronn

Mark is there any reason why the matrix would kill everything? I have it in a low variable with early parsing on just like you said (you said snippet anyway)

{exp:channel:entries channel="cars" dynamic="no" disable="member_data|pagination|category_fields"}

 {categories}

  {exp
:stash:append_list name='cars_{category_id}' parse_tags="yes" save="yes" scope="site"}

   {stash
:this_title}{title}{/stash:this_title}
   {stash
:this_description}{desc}{/stash:this_description}

   {stash
:this_main_img}
    {lv_matrix_img}
   {
/stash:this_main_img}

  {
/exp:stash:append_list}

 {
/categories}

{
/exp:channel:entries} 

With the LV (matrix inside) I get following showing up on the front end:

{categories} {/categories} {categories} {/categories} 

I only have two entries so I assume that’s why there are two categories pairs.

Everything in get_list just disappears completely.

As soon as I remove the low variable with the matrix tags everything works fine (minus the issue at hand of course.)

Any thoughts why this might be the case?

This is 2.3.2 by the way.

Thanks!

Natetronn
# 11
Natetronn

Mark,

This may be a bug come to find out!

At least according to Brandon over at Pixel and Tonic.

I’m double checking with him to see if he thinks this is in fact the same issue that I’m coming across:

https://getsatisfaction.com/pixelandtonic/topics/matrix_with_categories_tags#reply_4679554

What a bummer!

Thanks!

Mark Croxton
# 12
Developer
Mark Croxton

Yes I’ve come across the issue before, come to think of it. You could do the following to get round the problem of EE stripping out the {categories} tag pair:

{exp:channel:entries channel="cars" dynamic="no" disable="member_data|pagination|category_fields"}

{
!-- cache for 60 minutes --}
{exp
:stash:set_list name='cars' parse_tags="yes" save="yes" scope="site" replace="no" refresh="60"}

 {categories}

   {stash
:category_id}{category_id}}{/stash:category_id}
   {stash
:this_title}{title}{/stash:this_title}
   {stash
:this_description}{desc}{/stash:this_description}

   {stash
:this_main_img}
    {lv_matrix_img}
   {
/stash:this_main_img}
 {
/categories}
{
/exp:stash:set_list}

{
/exp:channel:entries} 

Use match/against to get like this:

{exp:channel:categories channel="cars" style="linear" disable="category_fields" show_empty="no"}
 
<h2>{category_name}</h2>
 
{exp:stash:get_list name="cars" scope="site" match="#^{category_id}$#" against="category_id"}
 {this_title}
 {
/exp:stash:get_list}
{
/exp:channel:categories} 
Natetronn
# 13
Natetronn

Mark,

Thanks, this looks like a great work around!

I will need to double check it to be working as I did away with the Matrix already. I will however try it and update this thread for others as a courtesy that it does in fact bypass the issue.

Loughlin
# 14
Loughlin

I’m just chiming in here to say this is a great solution to listing categories and associated entries - thanks Mark for the great support you provide for Stash.