1

I'm trying to set (and change) a Stash var within a search loop. I tried process='end', parse='inward' and similar, but i can't set/update/replace the var inside the loop and get the proper value back:

{exp:search_fields search:chapter_keywords='{embed:search}' search:chapter_text='{embed:search}' operator="OR" channel="chapter" parse="inward"}
    {exp:channel:entries entry_id="{search_results}" orderby="chapter_name" sort="asc" limit="50" dynamic="no" disable="categories|category_fields|member_data|pagination"}

        {exp:stash:set name="chapterType" parse="inward" parse_tags="yes" trim="yes"}unknown{/exp:stash:set}        
        {chapter_videos limit="1"}  
            {if chapter_videos:count > 0}
                {exp:stash:set name="chapterType" parse="inward" parse_tags="yes" trim="yes" replace="yes"}video{/exp:stash:set}            
            {/if}
        {/chapter_videos}   


        {chapter_images limit="1"}
            {if chapter_images:count > 0}       
                    {exp:stash:set name="chapterType" parse="inward" parse_tags="yes" trim="yes" replace="yes"}image{/exp:stash:set}    
            {/if}
        {/chapter_images}

          {embed="_chapters/index" entryId="{entry_id}" chapterType="{exp:stash:get name='chapterType' process='end'}{/exp:stash:get}"}             

Not closing exp: here, but they are closed for real.

{exp:stash:get name='chapterType' process='end'}{/exp:stash:get}

always returns 'image'

{exp:stash:get name='chapterType'}{/exp:stash:get}

always returns 'video'

When i output the chapterType within the loop without try to set it with Stash it'll print the correct vars (image, video, unknown) depending on the search results/loop entries.

2 Answers 2

0

Looks like you've got a bit going on there. It's a parse order issue, the way EE parses content takes some time to get your head around.

First up, your testing results for up to 50 items, so using a simple stash:set will only ever list the last result.You'll need to use the stash:set_list instead. I'd avoid using the embed where you can as in this situation with the code it will bog the system down as it will embed 50 times (if 50 results).

I'm not 100% sure of this code, but hopefully will help you on your journey :)

{!-- Set a stash_list of results, parse_depth is 3 to drill down through the various tags/modules --}
{exp:stash:set_list name="results" parse_tags="yes" parse_conditionals="yes" trim="yes" parse_depth="3"}

    {exp:search_fields search:chapter_keywords='{embed:search}' search:chapter_text='{embed:search}' operator="OR" channel="chapter" parse="inward"}

        {exp:channel:entries entry_id="{search_results}" orderby="chapter_name" sort="asc" limit="50" dynamic="no" disable="categories|category_fields|member_data|pagination"}
            {!-- Capture the entry id to a stash and use in get_list below --}
            {stash:entryid}{entry_id}{/stash:entryid} 
            {!-- Set stash to wrap the results, which ever tests true will be recorded (if both are true, image will overwrite video --} 
            {stash:chapterType}
                {chapter_videos limit="1"}  
                    {if chapter_videos:count > 0}video{/if}
                {/chapter_videos}   

                {chapter_images limit="1"}
                    {if chapter_images:count > 0}image{/if}
                {/chapter_images}
            {/stash:chapterType} 

        {/exp:channel:entries}

    {/exp:search_fields}

{/exp:stash:set}


{exp:stash:get_list name="results"}
{!-- Pass the stash list to the embed below (avoid embed if possible) --}
{embed="_chapters/index" entryId="{stash:entryid}" chapterType="{stash:chapterType}"}
{/exp:stash:get_list}
0
0

That seems to do the job! Thanks. Few notes:

  1. No need to use {stash: within the {exp:stash:get_list
  2. {exp:stash:set_list needs to be closed with {/exp:stash:set} not just {/exp:stash:set}

With this corrections your code works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.