rsanchez/mo_variables

Check for GET/POST variable

Closed this issue · 5 comments

How would you check if there is a GET or POST variable present in the URL?

For example, if I have the URL http://domain.com/calendar/week/?calendar=429 and the following in my template:

{if {get:calendar} == ""}
<h1>No calendar selected</h1>
{if:else}
<h1>Calendar ID selected: {get:calendar}</h1>
{/if}

It will correctly output "Calendar ID selected: 429". However, if the URL is just http://domain.com/calendar/ it outputs the literal string "Calendar ID selected: {get:calendar}". I can't test for the presence of GET variables in a URL because the test {if {get:calendar} == ""} will never evaluate as true.

Try one of the following:

{if get:calendar}
<h1>Calendar ID selected: {get:calendar}</h1>
{if:else}
<h1>No calendar selected</h1>
{/if}
{if "{get:calendar}" = ""}
<h1>No calendar selected</h1>
{if:else}
<h1>Calendar ID selected: {get:calendar}</h1>
{/if}

The first example you gave works well. Thank you.

However, when I try to apply it to something a little more complex, I get errors.

This works if there is a GET variable present:
{if get:calendar}
{exp:calendar:cal calendar_id="{get:calendar}" enable="custom_fields" pad_short_weeks="y"
{if freebie_3 == ''}
date_range_start="0 weeks begin"
{/if}
{if freebie_3 != ''}
date_range_start="{freebie_3}-{freebie_4}-{freebie_5}"
date_range_end="{freebie_3}-{freebie_4}-{freebie_5}"
{/if}
}
{if:else}
{exp:calendar:cal enable="custom_fields" pad_short_weeks="y"
{if freebie_3 == ''}
date_range_start="0 weeks begin"
{/if}
{if freebie_3 != ''}
date_range_start="{freebie_3}-{freebie_4}-{freebie_5}"
date_range_end="{freebie_3}-{freebie_4}-{freebie_5}"
{/if}
}
{/if}

However, if there is no GET variable present, I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':calendar})...

Given the error, it seems like it's evaluating the string {get:calendar} in the calendar_id parameter which means {if get:calendar} is testing true even without a GET variable.

This is an issue with EE's advanced conditionals, which actually get parsed after tag parsing. To get around this, you can either get rid of the if:else (demonstrated below), or you can use the if:else plugin.

{if get:calendar}
   //etc
{/if}
{if get:calendar == 0}
   //etc
{/if}

In general the recommendation is to avoid advanced conditionals (anything with if:else, && || OR AND) when you can, particularly if you have nested plugin tags within them.

Thank you. That was a tremendous help.

My first EE site so lots to learn.

The parse order stuff is really good to know. This is the sacred document of EE parse order: http://loweblog.com/downloads/ee-parse-order.pdf