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.

Strange Logic

May 04, 2011 2:37pm

Subscribe [4]
  • #1 / May 04, 2011 2:37pm

    Loquela

    105 posts

    Hi there,

    Why does this work:

    {if url_title == '{segment_2}’}

    But this doesn’t?

    {if segment_2 == '{url_title}’}

    Thanks in advance,

    L.

  • #2 / May 04, 2011 3:22pm

    Neil Evans

    1403 posts

    in short i dunno… but i bet this would.

    {if "{url_title}" == "{segment_2}"}
    {if "{segment_2}" == "{url_title}"}
  • #3 / May 04, 2011 4:22pm

    Boyink!

    5011 posts

    Did you try:

    {if url_title == segment_2}
  • #4 / May 04, 2011 4:27pm

    Loquela

    105 posts

    Thanks both,

    I’m actually able to achieve what I want with this:

    {if url_title == ‘{segment_2}’}

    My question is, why does that above work, when the one below does not?

    {if segment_2 == ‘{url_title}’}
  • #5 / May 04, 2011 4:32pm

    Boyink!

    5011 posts

    Because putting quotes around variable names can cause the conditional to check against the literal value rather than the contents of the variable. I avoid it generally.

  • #6 / May 04, 2011 5:01pm

    Loquela

    105 posts

    I’m not sure I follow this logic either?

    This is why I call it Strange Logic.

    Because this works (note the ‘{segment_2}’:

    {if url_title == ‘{segment_2}’}

    But this doesn’t (note the ‘{url_title}’):

    {if segment_2 == ‘{url_title}’}

    ???

  • #7 / May 04, 2011 5:06pm

    Boyink!

    5011 posts

    I’ve had inconsistent results as well - and just never took the time to investigate them once I found that dropping all quotes was more reliable..😉

    IIRC there was even a security risk in quoting variables - if the contents of the variable might contain user-provided content.

  • #8 / May 04, 2011 5:10pm

    Loquela

    105 posts

    OK, thanks Boyink!

    L.

  • #9 / May 05, 2011 2:18am

    John Henry Donovan

    12339 posts

    Loquela,

    It’s a parsing issue you run into I believe. A segment variable is parsed first and then a simple conditional

    {if url_title == '{segment_2}'}

    Doing it this way makes sure you are using the pre-parsed segment variable. The same way this might work

    {if "{segment_2}" == "{url_title}"}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases