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

     

Stash inside conditional inside channel:categories

Support Request

JUK
JUK

It’s my first foray with Stash, and I want to assign the category information to Stash variables.

This is my attempt:

{exp:channel:categories
 channel
="my_channel"
 
category_group="2"
 
style="linear"}

 {if category_url_title 
== segment_2}
  {exp
:stash:set}
   {stash
:category_id}{category_id}{/stash:category_id}
  {
/exp:stash:set}
 {
/if} 
{
/exp:channel:categories} 

I’m having two issues:

1. When I output the category_id just before the end of the channel:categories loop it has a value, but not outside it.

2. The value is always the same, even if “segment_2” changes.

What am I missing?

Mark Croxton
# 1
Developer
Mark Croxton

1. Module tags are parsed from the outside in, like peeling an onion. If you try to access a variable on the outer layer when it is set in a lower layer, it won’t exist at the point it is parsed.

2. Advanced conditionals are parsed *after* the content they wrap. So your variable will always be set to the last value.

It’s easy to solve this but tell me what do you want to do here? Your variable will be set on each iteration of the loop of the parent tag. What do you want to do with it?

JUK
# 2
JUK

Thanks for your reply!

I am trying to re-write the template for a sub-index page.

I have set up categories (like “Arts”, “Health”, etc.) and each of these has an index page below the overall topic (“Culture”).

Now I like to output the category’s title and description on the category (or sub-) index page. Previously I used a channel:categories tag pair and PHP to remember the values, but after reading about Stash and the new workflow I figure there must be a better way.

moki
# 3
moki

What is the easy way to have the advanced conditionals parsed early?  Do you mean using your plugin ifElse?

I’m having a similar issue with this code:

{exp:low_nice_date:range from="{current_time}" to="{expiration_date}"}
      {if days 
3}
       {exp
:stash:set_value name="icon_url" value="/ee/site/assets/googlemaps/marker-icon-orange.png"}
      {if
:elseif days 11}
       {exp
:stash:set_value name="icon_url" value="/ee/site/assets/googlemaps/marker-icon-red.png"}
      {if
:elseif days 31}
       {exp
:stash:set_value name="icon_url" value="/ee/site/assets/googlemaps/marker-icon-green.png"}
      {if
:else}
       {exp
:stash:set_value name="icon_url" value="/ee/site/assets/googlemaps/ulo_marker_blue.png"}
      {
/if}
     {
/exp:low_nice_date:range} 

Obviously, it’s always evaluating to the last one, since stash is parsed earlier than the advanced conditionals.  Which is not what I want.

I’ve tried using

{exp:stash:parse} 

in every permutation I can think of as per:

https://gist.github.com/croxton/9d012297096892ca5c10

(I’m using EE 2.7.3 btw), but no dice.

Mark Croxton
# 4
Developer
Mark Croxton

Wrap a stash set *around* the low_nice_date tag pair and use the parse=“yes” parameter.

moki
# 5
moki

Thanks Mark.  I ended up saying “screw it” and used PHP instead.  i will try your technique in the future, however.