2

So, I think I've caught on that advanced conditions and EE tags don't play like expected.

So, I downloaded Mark C's "IfElse" plugin and things are looking up.

However, my default condition, doesn't work. EE is NOT parsing the "last/default" condition so therefore the content is not appearing from the channel and only text versions of the field names (e.g. {title}, {body}) appear.

Any help would be appreciated.

{exp:ifelse parse="inward"}               

        {if segment_3 =="1"}
           {exp:channel:entries channel ="about" entry_id="33"} 

        {if:elseif segment_3=="2"} 
           {exp:channel:entries channel ="about" entry_id="32"} 

        {if:elseif segment_3=="3"}
           {exp:channel:entries channel ="about" entry_id="34"} 

        {if:elseif segment_3=="4"}
           {exp:channel:entries channel ="about" entry_id="35"}     

        {/if}
{/exp:ifelse}

{title}
{body}

{/exp:channel:entries}

Also, for the last condition, I've tried the normal:

 {if:else}
   {exp:channel:entries channel ="about" entry_id="35"}     

 {/if}
 {/exp:ifelse}

But no cigar.

  • 1
    If you just put in "test" inside of the last {if:else} does that not appear either? – Stephen Callender Jun 13 '14 at 22:39
  • 2
    Where, on your code, are you closing the {exp:channel:entries}? Please, update your question. – Robson Sobral Jun 13 '14 at 23:15
  • Robson, updated post with entries tag... – therooster Jun 14 '14 at 8:48
  • Stephen, "test" does not appear. – therooster Jun 14 '14 at 8:51
1

You don't need any add-on to make this to work.

Take a look at the docs.

Segment variables as parsed before any module tags. Advanced conditionals are parsed after, but you can use simple conditionals.

So:

{exp:channel:entries channel ="about"
    {if segment_3 == "1"}
        entry_id="33"
    {/if}
    {if segment_3 == "2"}
        entry_id="32"
    {/if}
    {if segment_3 == "3"}
        entry_id="34"
    {/if}
    {if segment_3 == "4"}
       entry_id="35"
    {/if}
} 

    {title}
    {body}

{/exp:channel:entries}

Isn't all that beautiful, I know, but is fast, simple and DRY.

| improve this answer | |
  • yeah! I like that also. I'm really not a fan of using addson unless necessary. But I do like stash. However, native is always my first choice if all things are equal. – therooster Jun 16 '14 at 16:48
  • That's my case too, @therooster . Add-ons tend to over complicate things, sometimes. – Robson Sobral Jun 16 '14 at 16:57
  • @therooster, can you, please, mark the right answer? I know that all the three are right, but it will make easier for us to find question still waiting for answers. I don't need to pick mine. Thanks! – Robson Sobral Jun 21 '14 at 4:05
2

Yikes! With all due respect, this is an extremely inelegant approach. You should never attempt to output different opening tags in this way. Even if you are able to somehow make it work, the next developer to look at this code will be cursing your name!

Here are some alternatives:

  • Use the url_titles or the entry_ids directly in segment_3 instead of 1/2/3, which would automatically show the right entry due to the default dynamic nature of the channel:entries tag.

  • Use the native Pages module to assign whatever URI/template combination you like to an entry, which will also allow a single channel entries tag to work dynamically

  • Alternatively, use Stash to set a variable which you then pass to the channel:entries tag like so:

--

{exp:stash:set name="the-entry"}
    {if segment_3 == '1'}31{/if}
    {if segment_3 == '2'}32{/if}
    {if segment_3 == '3'}34{/if}
    {if segment_3 == '4'}35{/if}
{/exp:stash:set}

{exp:stash:parse}
    {exp:channel:entries entry_id="{stash:the-entry}"}
        <h1>{title}</h1>
        {body}
    {/exp:channel:entries}
{/exp:stash:parse}
| improve this answer | |
  • James, your stash approach works good. never used Stash before so this is a good approach to add to the tool set. Thanks! – therooster Jun 14 '14 at 13:32
  • also, fyi... in my real code i did use the url_titles directly in segment 3. the 1,2,3, i was using to create a simply example to ask a question. just fyi, for folks if they have a similar issue. – therooster Jun 14 '14 at 13:35
  • If you're using the url_title in segment 3 you shouldn't need to do any of this - the channel:entries tag will dynamically identify the right entry to use. – James Smith Jun 14 '14 at 16:35
2

As someone else has said this is an inelegant approach and there are better ways to do it.

But for starters try putting the complete tag pair within each condition and see if that works. i.e.

    {if segment_3 =="1"}
       {exp:channel:entries channel ="about" entry_id="33"} 
           stuff
       {/exp:channel:entries}
    {if:elseif segment_3=="2"} 
       {exp:channel:entries channel ="about" entry_id="32"} 
           stuff
       {/exp:channel:entries}
    {if:elseif segment_3=="3"}
       {exp:channel:entries channel ="about" entry_id="34"} 
           stuff
       {/exp:channel:entries}
    (if:elseif segment_3=="4"}
       {exp:channel:entries channel ="about" entry_id="35"}
           stuff
       {/exp:channel:entries}
    {/if}     

If that works you can then think about how to make the code more efficient. Try using stash, switchee or as someone said, use the entry IDs/url_title in your URL which will negate the need for this approach.

| improve this answer | |
  • foamcowm, the code i placed earlier was an example. i have too much code on the real page before the closing entries tag, so putting everything between tags would be a nightmare. – therooster Jun 14 '14 at 13:29
  • However, I do think the Stash approach would be good. Never used stash, so can't wait. – therooster Jun 14 '14 at 13:32
  • Because these are "advanced" conditionals, every single one of those channel entries tags will run before the final output is filtered, so you really should avoid doing this. – James Smith Jun 14 '14 at 16:34
  • Agreed but it would get it working as a starting point. – foamcow Jun 14 '14 at 21:22

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.