We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Cookie value to determine conditional logic (if statement)

Development and Programming

cl-tech's avatar
cl-tech
1 posts
14 years ago
cl-tech's avatar cl-tech

I’m working within a snippet and I’m trying to output content based on a cookie being set or not. I don’t understand the expression engine documentation regarding cookies.

There are no examples of conditional logic and detecting cookies.

I’m setting the cookie via JavaScript on a form submit. I want to make it a bit more elegant and not render unnecessary code in the page that’s currently hidden via JavaScript when it could be determined server side.

Am i right in using:

$this->EE->input->cookie();

I would expect to do something like:

{if $this->EE->input->cookie("my_Cookie"); == "my_Cookie" }

Do Something here

{/if}

I tried using the cookies_plus plugin but I couldn’t get that to work.

A prod in the right direction would be fantastic.

Moved to Development and Programming by Moderator

       
Lee Powell's avatar
Lee Powell
88 posts
14 years ago
Lee Powell's avatar Lee Powell

You are mixing PHP in with template tags… I’d suggest taking a more detailed read of the documentation.

If you want to do any PHP in a template, you’ll need to enable PHP parsing for that template in the Template Manager. Something like this may work better:

<?php
if( $this->EE->input->cookie("my_Cookie") == "my_Cookie" ):
?>
Do something here
<?php
endif;
?>
       
cl-tech's avatar
cl-tech
1 posts
14 years ago
cl-tech's avatar cl-tech

You put me on the right track with the right php syntax of outputting code between tags. I tried numerous variations but I couldn’t retrieve the cookie value using expression engine’s built in syntax so I used php all the way.

My solution:

<?php
if ( isset($_COOKIE["agreed_to_terms"]) != "1" ):
?>
Show persistent overlay until checkbox is ticked allowing user to access rest of website
<?php
endif;
?>

Oddly, as a developer, php makes more sense to me to implement than some of expression engine’s syntax. I realise I need to do my homework regarding modules and input and function classes but was given a site to put together in two weeks with no prior knowledge of expression engine.

Thank you for your help Lee

       
Lee Powell's avatar
Lee Powell
88 posts
14 years ago
Lee Powell's avatar Lee Powell

Yes, as a PHP dev I understand where you’re coming from. However template tags are there for non-developers and optimise parsing of the templates. Personally, if I find I’m using PHP in a template, I try to abstract that away into a separate plugin.

It wouldn’t take you long to create your own cookie plugin based on what you need. Then, on future projects, it’s more easily reusable. Something like:

{if {exp:cookie_monster name="agreed_to_terms"} != "1"}
 ...
{/if}

Your plugin could simply return the value of a cookie if it exists, or false if it doesn’t.

       
Sue Crocker's avatar
Sue Crocker
26,054 posts
14 years ago
Sue Crocker's avatar Sue Crocker

Thanks for the assist, Lee.

Since this is more of a coding question, moving to the Development forum for additional community support.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.