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 variable as search parameter in exp:cal:events tag

Support Request

calebdurham
calebdurham

I’m stumped on something. I’m trying to (using AJAX) allow users to filter events by their location. So you can choose either Region or Country from selects that are populated with the addon REEgion Select. Whatever you select then becomes segment_3. Pretty straightforward. If you chose a Region, it’s no problem to just use that segment as the search, but because there’s some overlap between the two letter codes (CA is Canada and California), I created a conditional to determine what is being returned, and am trying to use stash to get that conditional variable into the search function of the {exp:calendar:events} tag.

This is that tag:

{exp:stash:parse process="end"}
    {exp
:calendar:events event_limit="30" channel="calendar_events" date_range_start="today" date_range_end="1 year" dynamic="no" search:address="{stash:region_search}"}
     
<a href="#">{title}</a>
    
{/exp:calendar:events}
{
/exp:stash:parse} 

Here’s the stash variables I created:

{exp:stash:set name="region_stash"}{exp:reegion_select:countries}{if segment_3 == "{region_alpha3}"}{region_alpha2}{/if}{/exp:reegion_select:countries}{/exp:stash:set}

{exp
:stash:set name="region_search" trim="yes"} {if '{exp:stash:get name="region_stash" random}' == ''"region":"{segment_3}" {if:else} "country":"{exp:stash:get name="region_stash" random}" {/if} {/exp:stash:set}

{exp
:stash:get name="region_search" random} 

outside of the tag, this stashed variable works great and renders no problem. Do you know what I’m doing wrong?

Thank you!

Mark Croxton
# 1
Developer
Mark Croxton

You need to add parse=“yes” here or all you’re doing is capturing the literal tags and conditionals themselves rather than their output. Make the variable type=“snippet” so that we can use native EE conditionals with it later on, without having to enclose in quotes:

{exp:stash:set name="region_stash" type="snippet" parse="yes"}{exp:reegion_select:countries}{if segment_3 == "{region_alpha3}"}{region_alpha2}{/if}{/exp:reegion_select:countries}{/exp:stash:set} 

And parse_conditionals=“yes” here (b/c you only need conditionals parsed), and this time you can use the snippet variable rather than tags in your conditional. Your conditional is the wrong way round I think, but I’m also not sure why you’re enclosing your search in quotes, but please adjust this to whatever you think the output should be:

{exp:stash:set name="region_search" trim="yes" parse_conditionals="yes"} {if region_stash}region:{region_stash}{if:else}country:{segment_3} {/if} {/exp:stash:set} 
calebdurham
# 2
calebdurham

Thanks so much mark! Initially, this didn’t work for me, but it definitely pointed me in the right direction. Eventually, I realized I did need to parse region_search as more than just conditionals, and when I applied the rest of what you said with parse=“yes”, I got it working. I really appreciate your support.