ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Too many if:elseif conditionals breaks my code

November 01, 2011 3:03am

Subscribe [3]
  • #1 / Nov 01, 2011 3:03am

    Austmel

    4 posts

    Hi All,

    I have a right pane which is embedded in my main template it has a lot of if:elseif conditionals based on segments, the conitionals look at the segment name and then displays the corresponding entries based on the categories that are associated.

    If i remove 1 set of if:elseif conditions the code works fine but as soon as i add it back the code breaks, i have checked the code time and time again and it should work fine.

    Can someone please take a look at the attached code.

    thanks for any assistance.

    [Mod Edit: Moved to the EE 2 Tech Support forum]

  • #2 / Nov 01, 2011 12:11pm

    Ian Ebden

    312 posts

    If it were me, I’d break this whole thing up a bit. Conditionals – especially nested ones – can be a real resource drain. You might consider using snippets named after your segments, maybe something like…

    {if segment_2}{sn_literature_{segment_2}}{/if}

    This would grab a snippet named “sn_literature_packaging_products” for example. This approach would make your engine run a little smoother as well as perhaps making it easier for you to understand and edit that content in future?

    An alternative might be to pass variables into an embedded template, although I prefer snippets. You could also take a look at a pretty could case/switching add-on called Switchee, which is really great at creating lightweight cases (as you would in PHP) rather than conditions – just search in the Devot-ee store for this add-on.

    Hope that helps?

  • #3 / Nov 01, 2011 8:05pm

    Austmel

    4 posts

    Hi,

    That is a great idea although i have one small problem with it when i use your code

    {if segment_2}{sn_literature_{segment_2}}{/if}

    i get {sn_literature_packaging_products} text returned in my right pane rather than the results of running the snippet. It seems like it doesn’t process the snippet. if i change your code to

    {if segment_2}{sn_literature_packaging_products}{/if}

    the snippet is processed and i get the results i’m after.

    Any idea why the original code does not process the snippet?

  • #4 / Nov 02, 2011 6:38am

    Ian Ebden

    312 posts

    Hmm… in that case I’m a little stumped. Code is fine and as long as you’ve got a snippet called “sn_literature_packaging_products” you should be fine. It’s a technique I use quite a bit.

    Could be a parse order issue. Are you running this from an embedded template maybe? Not sure that would affect it but worth asking.

  • #5 / Nov 02, 2011 1:34pm

    Sue Crocker

    26054 posts

    Thanks for the assist, Ian.

    Austmel, are you running this from an embedded template like Ian mentioned?

  • #6 / Nov 02, 2011 6:31pm

    Austmel

    4 posts

    Hi,

    Yes this is running from an embedded template.

    I have my main products template, then a right pane template which is embedded, the right pane template contains Ian’s code.

    I guess i can revert and place this code in the main template if there is no other way?

    cheers,

    Ben.

  • #7 / Nov 04, 2011 2:19pm

    Sue Crocker

    26054 posts

    Hi, Ben. I’d just code it in the main template. Feel free to start a new thread if you have any more questions.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases