1

Assume the URL: www.domain.com. What is {segment_1}? It's empty, right?

Now assume the URL: www.domain.com/foo/. What is {segment_1}? It's foo, right?

I have this in my template:

<h1>{if segment_1 == ''}{segment_1} - case 1{/if}</h1>
<h1>{if segment_1 == 'foo'}{segment_1} - case 2{/if}</h1>

On www.domain.com, I get an output of - case 1 wrapped in h1 tags followed by an empty pair of h1 tags. This seems correct to me.

On www.domain.com/foo/, I get an output of foo - case 1 wrapped in h1 tags followed again by an empty pair of h1 tags. This is wrong. I should be getting an empty pair of h1 tags followed by foo - case 2. Any idea what's going wrong with my conditionals?

I'm using latest version of EE. Using Stash, too.

  • Your assumption is correct. Are you using any add-ons that manipulate segments, like Structure or Freebie? If you add {segment_1} somewhere globally in your template, does it display properly throughout your site? – Derek Hogue Jan 10 '13 at 14:18
  • Don't think I have anything manipulating segments. My .htaccess removed index.php; not sure if that matters. I will test further and get back to you. – ExpressionEngineNewbie Jan 10 '13 at 14:30
  • But I really thing the value of {segment_1} is correct. My sample code shows that. The first if statement is always true; subsequent if statements are always false. I think that might be an accurate description of the problem as I see it right now. – ExpressionEngineNewbie Jan 10 '13 at 14:38
  • If you remove the first conditional, and just have {if segment_1 == 'foo'}{segment_1} - case 2{/if}, does the output match your expectations? – Derek Hogue Jan 10 '13 at 14:52
  • 1
    Oh my - the problem no longer exist. The markup was actually being embedded by Stash with process="start". It was actually a chain of embeds. I removed process="start" and now it's working. But I might have been doing other things too, so not 100% this was a Stash issue. I'll test some more later. – ExpressionEngineNewbie Jan 10 '13 at 14:59
3

I think the problem may lie in that you are testing the 1st segment, which according to the user guide should be the name of your template group.

If the template group your template is in is the default template group you can leave it out of the URL structure. But then EE will think that foo is a template in the default template group and try and load it.

If foo isn't a template it will trigger the 404 template if it has been defined, if not it will use the default template group index template. So depending on what code you have in that will determine what happens next.

There is an excellent flow chart of how segments are processed by EE by @_jamessmith that is essential reading when dealing with templates and segments.

If you put that conditional in a template named foo that is in the default template group and change it to test segment 2 it works fine.

-2

Your H1 tags need to be inside the conditional:

{if segment_1 == ''}<h1>{segment_1} - case 1</h1>{/if}
{if segment_1 == 'foo'}<h1>{segment_1} - case 2</h1>{/if}
  • Actually not answering his question, though it is a little bit convoluted. He's asking about the conditional comparison, not about properly showing/hiding the tags. – Derek Hogue Jan 10 '13 at 14:15
  • Right, the example was to simplify my problem (it was actually the test code I put together to verify the problem). I'm trying to use this to set an active class in my site's navigation, but it wasn't working. – ExpressionEngineNewbie Jan 10 '13 at 14:31

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.