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
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;
?>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
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.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.