0

Inside this URL there is a parameter:

http://localhost/recipe?id=9

Is it possible to use this parameter (id=9) with a conditional, like a segment? Or to check if there is a parameter inside a URL or not?

3 Answers 3

1

You can easily retreive GET and POST variables with the Mo' Variables add-on.

Download from Devot-ee
Github repository

For example, if your URL is http://localhost/recipe?id=9, you could use this code in your recipe/index template:

{if get:id == "9"}
  TRUE!
{/if}

You can also create default values for variables in the extension settings to test conditionals if no id parameter exists. For instance, if the default value for "id" is 0:

{if get:id != 0}
  ID EXISTS!
{if:else}   
  No ID here!
{/if}

This is useful for handling exceptions like if there is no value in the query string (/recipe?id=).

5
  • Thanks for your answer Alex. Why using {if "{get:id}" == "9"} in place of {if {get:id} == "9"}. In this case what is the role of the Quotation marks between {get:id}???
    – Stéphane
    Mar 18, 2014 at 12:19
  • Using {if "{get:id}" != ""} works fine, but without the quotation marks: {if {get:id} != ""} give a server error. I'm just curious to know why this error appears without the the quotation marks.
    – Stéphane
    Mar 18, 2014 at 19:01
  • Honestly, I'm not sure why conditionals work without quotation marks sometimes and other times you need them. Testing {if {get:id} == "9"} works fine. So does {if get:id == "9"}. And even {if {get:id} == 9} if you only ever expect an integer as the value. But I usually try using quotes and curly braces as part of my troubleshooting process because they tend work more often especially for complicated logic. Reading thru this related question, it seems like there is not clear consensus on the issue: expressionengine.stackexchange.com/q/7/153
    – Alex Roper
    Mar 18, 2014 at 20:29
  • Turns out, it's fine to not use curly braces or quotes and you may even have befits from this in terms of the parse order so I'll update my answer.
    – Alex Roper
    Mar 18, 2014 at 22:09
  • Alex, thanks a lot for your explication and for the link. It's more clear now!!
    – Stéphane
    Mar 19, 2014 at 0:02
1

With mo' variables (http://devot-ee.com/add-ons/mo-variables) you can get your get/post vars into early parsed variables you can then use in standard conditionals.

1

The URL Helper add-on is also worth mentioning, and will give you access to the query string within your templates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.