1

I'm having some problems with my view model template... I'm trying to compare the value of url_title, but regardless of the value of it the last conditional passes and sets the stash variable...

This is what I Have:

{stash:embed:layouts:full-width}

{exp:channel:entries limit="1" channel="pages" disable="member_data|pagination" require_entry="yes" dynamic="no" url_title="{segment_1}" parse="inward"}
    {exp:stash:set_value name="content" value="{exp:stash:embed:partials:supports-landing}"}
    {exp:stash:set_value name="body_class" value="{url_title}"}

    {if "{url_title}" == "supports-for-adults"}
        {exp:stash:set_value name="support_type" value="Adults"}
    {if:elseif "{url_title}" == "supports-for-children"}
        {exp:stash:set_value name="support_type" value="Children"}
    {/if}

    {exp:stash:set_list
        name="supports-landing"
        parse_tags="yes"
    }
        {stash:entry_id}{entry_id}{/stash:entry_id}
        {stash:body}{body}{/stash:body}
        {stash:support_type}{exp:stash:support_type}{/stash:support_type}
    {/exp:stash:set_list}
{/exp:channel:entries}

{exp:stash:set_list
    name="supports-landing-list"
    parse_tags="yes"
}
    {exp:channel:entries channel="supports" disable="member_data|pagination" dynamic="no" orderby="title" sort="asc"}
        {if "{supports_for}" == "{exp:stash:support_type}"}
            {stash:support_title}{title}{/stash:support_title}
            {stash:support_excerpt}{supports_excerpt}{/stash:support_excerpt}
            {stash:support_cover_image}{supports_cover_image}{/stash:support_cover_image}
            {stash:support_url}{path='supports/{url_title}'}{/stash:support_url}
        {/if}
    {/exp:channel:entries}
{/exp:stash:set_list}

Every time, regardless if i'm on /supports-for-adults or /supports-for-children, the value of {exp:stash:support_type} will be Children. If I remove the if:elseif part of the conditional, it will always be Adults.

Any ideas why this would be happening, been fighting with it for quite some time.. Tried removing the "'s, the {'s, tried using the IfElse plugin, tried using Switchee... Tried changing the exp:channel:entries to dynamic="no", parse="inward", url_title="{segment_1}" etc.. I can't think of anything else to try now...

1

Since you don't have a default {if:else} case, try using simple conditionals compared against segment_1 (since that's setting your entry anyway).

{if segment_1 == "supports-for-adults"}
    {exp:stash:set_value name="support_type" value="Adults"}
{/if}
{if segment_1 == "supports-for-children"}
    {exp:stash:set_value name="support_type" value="Children"}
{/if}
  • I tried this as well, except with {url_title}. I'll have to try segment_1 when I get to work tomorrow. Not sure if it would make a difference or not. – marcaddeo Nov 6 '13 at 4:03
  • @jackyyll How did this treat you? – Stephen Callender Nov 14 '13 at 14:22
1

Get yourself a copy of ifelse by Croxton - this will evaluate your conditionals but only output the condition that is met. OOB EE conditionals evaluate everything and then displays what is met.

so your code would look like this

{exp:ifelse parse="inward"} 
    {if "{url_title}" == "supports-for-adults"}
        {exp:stash:set_value name="support_type" value="Adults"}
    {if:elseif "{url_title}" == "supports-for-children"}
        {exp:stash:set_value name="support_type" value="Children"}
    {/if}
{/exp:ifelse}
0

As I understand the parsing order of EE it actually parses all items within the conditionals and then decides which is correct and outputs that resulting code. So it is always parsing both stash values which is why you always get the second value as the result. Try using http://devot-ee.com/add-ons/switchee for your conditionals instead.

{exp:switchee variable="{url_title}" parse="inward"}
  {case value="supports-for-adults"}
    {exp:stash:set_value name="support_type" value="Adults"}
  {/case}
  {case value="supports-for-children"}
    {exp:stash:set_value name="support_type" value="Children"}
  {/case}
{/exp:switchee}

Switchee only parses the code within the case that matches so you will not overwrite your stash value as you are with native conditionals. Switchee can give quite a performance boost to your page if you are using exp:channel:entries calls within conditionals as well because you avoid needless calls to the database. You can also do regex matches.

  • I also tried using switchee, and that didn't work as well.. :/ – marcaddeo Nov 6 '13 at 13:46
  • Have you tried simple debugging like running just that portion of the code that is giving you difficulty without all the rest of it and instead of using stash just using something like <h2>The value will be set to Children</h2> – Phil Nov 6 '13 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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