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.

Can't get if conditionals to work using segments

November 28, 2011 11:21am

Subscribe [4]
  • #1 / Nov 28, 2011 11:21am

    Eelco

    23 posts

    Hi all,

    I’ve this strange problem with usings segments.

    This is what I have:

    {if segment_1 == "work" && segment_2 == ""}
    
     (page content here)
     
    {if:elseif segment_1 == "work" && segment_2 != ""}
    
     (page content here)
     
    {/if}

    The page with the first segment “work” is showing, but as soon as I try to get another segment in, I get a 404. I’m probably missing out on something here, because I had it working (on other sites) before. I also tried hard coding segment_2 in, but no luck.

    Additional info:
    v2.2.2 - Build: date 20110801
    the giving code is on a “page” (from the page module).

  • #2 / Nov 29, 2011 2:07am

    John Henry Donovan

    12339 posts

    Hi Eelco,

    Does this work for you?

    {if segment_1 == "work" && segment_2 == ""}
    
     (page content here)
    
    {/if} 
     
    {if segment_1 == "work" && segment_2 != ""}
    
     (page content here)
     
    {/if}
  • #3 / Nov 29, 2011 4:06am

    Eelco

    23 posts

    Unfortunately , no, but thanks. Same output.

    I’ve the exact same code working on an other site, except for that code not being on a page.

  • #4 / Nov 29, 2011 3:05pm

    Eelco

    23 posts

    If it’s any help to you: I’ve found that when even hardcoding the segment_2 for an entry that dóes exist, I still get an 404.

  • #5 / Nov 30, 2011 5:03pm

    Dan Decker

    7338 posts

    Hi Eelco,
    I did this test, where segment_2 is the name of my template, and it worked.

    Generally segment_1 should be the name of your template group, so segment_2 wouldn’t be an entry_id, that would be in segment_3.

    {if segment_2 == "segment-test" && segment_3 == ""}
    
     2 is empty
    
    {/if} 
     
    {if segment_2 == "segment-test" && segment_3 != ""}
    
     2 is not empty! w00t!!
     
    {/if}

     

  • #6 / Nov 30, 2011 5:43pm

    Eelco

    23 posts

    Thanks Dan, but it doesn’t work as described.

    FYI: in my case segment_1 is the name of the page “work”. On this page I’ve a list of projects, hailing from a channel called “projects”. Clicking on one of the projects gives you the url “work/project-name”, and thus segment_2 isn’t empty and therefor an other page should show up.

    I still think the problem relies with it being used on a page—rather than an entry.

  • #7 / Dec 01, 2011 8:59am

    Eelco

    23 posts

    I’ve tried—since I’m running out of time with this project—Switchee. Which is a nice solutions for these kind of situations anyway, but get the same output, error 404.

    Thinking more of this as being a bug in the system :-(

  • #8 / Dec 03, 2011 8:34am

    Eelco

    23 posts

    Too bad that not one of the experts did got the answer—this really is an area for improvement for a payed system like EE.

    For anyone interested—after loads of unpaid time—I’ve found out that’s it impossible to get if/else statements with segments to work on pages from the page module, be it a bug or not.

    So:

    {if segment_1 == "work" && segment_2 == ""}
    
     (page content here)
     
    {if:elseif segment_1 == "work" && segment_2 != ""}
    
     (page content here)
     
    {/if}

    DOES work when used on a template page (e.g. work/index), but DOESN’T work on pages from the page module (/work).

  • #9 / Dec 05, 2011 3:36pm

    Dan Decker

    7338 posts

    Hi Eelco,

    I apologize for any frustration you have over this. We truly do our best to resolve issues quickly. Since you are using this code on a template associated with a page, you won’t have access to standard segment variables like you would for standard template use. However, there is a substitute variable available, {page_uri} It contains the value of the page uri as assigned in the pages tab for the channel entry. It can’t be tested on a “segment” basis. You could use PHP or a plugin like PHPStringFun to break down the {page_uri} variable and then test against that.

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

ExpressionEngine News!

#eecms, #events, #releases