Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using :check in if statement does not work. #3

Open
robhuska opened this issue Jan 9, 2020 · 14 comments
Open

Using :check in if statement does not work. #3

robhuska opened this issue Jan 9, 2020 · 14 comments
Labels
bug Something isn't working

Comments

@robhuska
Copy link

robhuska commented Jan 9, 2020

Hello, thank you for your Addon, I am really hoping to get this worked out because this will be a fantastic addition if I can make this work... I have the below code in my template, but it will only ever get into the first part of the if. Even after setting the cookie and on refresh it never makes it into the else. Is there something i am missing or doing incorrect?

{{ if ! { cookie_monster:check key="welcome" } }}

	{{ cookie_monster:put key="welcome" value="hello" expires="30 days" }}

	{{ greetings where="initial:true" shuffle="true" limit="1" }}
		<span class="hero__greeting-message">{{ greeting }}</span>
	{{ /greetings }}   

{{ else }}
					
	{{ greetings where="initial:false" shuffle="true" limit="1" }}
		<span class="hero__greeting-message">{{ greeting }}</span>
	{{ /greetings }}

{{ /if }}

Even trying to {{ cookie_monster:retrieve key="welcome" }} before the if statement, it will output the value of the cookie "hello" but still not run the else.

Thank you,
Rob

@aryehraber
Copy link
Contributor

Hi @robhuska! From looking at your snippet everything seems fine... 🤔

I'll try and find some time to open a project where I used this addon last and test the same logic.

@robhuska
Copy link
Author

robhuska commented Jan 13, 2020

@aryehraber Thank you! That would be greatly appreciated. If it helps, I am on the latest version of Statamic 2.

@aryehraber
Copy link
Contributor

Hey @robhuska, I just tested this myself and seems to work fine. I used this snippet (similar to yours):

{{ if ! {cookie_monster:check key="test"} }}
  Cookie not found...

  {{ cookie_monster:put key="test" value="hello" }}
{{ else }}
  Cookie found: {{ cookie_monster:retrieve key="test" }}
{{ /if }}

As you would expect, on first load I see the message "Cookie not found..." and then upon refreshing I see "Cookie found: hello".

I'm honestly not sure what to suggest on your end... Only difference I see (which I don't think will change anything) is spaces around the check tag: {{ if ! {cookie_monster:check key="welcome"} }}.

@aryehraber
Copy link
Contributor

I also just tested the inverse and works fine too:

{{ if {cookie_monster:check key="test2"} }}
  Cookie found: {{ cookie_monster:retrieve key="test2" }}
{{ else }}
  Cookie not found...

  {{ cookie_monster:put key="test2" value="hello" }}
{{ /if }}

Could there be some other caching at play?

@robhuska
Copy link
Author

I just tried removing the extra spaces and correct, that did not change anything... I am running a site locally if that makes a difference? such as localhost:3000 or mysite.local. Anything that would cause things to not work for local sites? I don't think caching, because I can output the contents of the cookie after it is set and I refresh the page.

@robhuska
Copy link
Author

And actually I just double check on our staging site, it is not working there either. So it is not a local thing.

@aryehraber
Copy link
Contributor

Ok cool, good you checked on staging too!

Are you able to share the codebase (or a reduced test case) so that I can test on my end? Probably the quickest way to get to the bottom of this (I hope)!

@robhuska
Copy link
Author

Yeah, I would be willing to do so. Just not sure the best way to do so.

@aryehraber
Copy link
Contributor

Assuming you're using default Statamic (just flat-files), you should be able to just zip up the project folder and send it over (might be worth removing any cached images and the local directory to reduce the overal project size).

If you don't want to share over GH (understandable) then you can email me at: [email protected], if the zip file is too large for an email you can use WeTransfer.

@robhuska
Copy link
Author

OK, I will get this packed up and set over in a few minutes! Thank you so much for your help!

@aryehraber
Copy link
Contributor

Ok great, I'll try and take a look tomorrow and get back to you if I find anything noteworthy.

@robhuska
Copy link
Author

Thanks, I am sending a wetransfer now. I stripped out most, but I wanted to leave as much as possible of the core site stuff incase that is where my issue lies. Let me know if/when you get the transfer.

@aryehraber
Copy link
Contributor

Hi @robhuska! Thanks for sending that over, I was able to get a better understanding of what is going on and it seems you've come across a Statamic bug! I'll be reporting this to their GH issues later this week, but you may not get a fix for this soon.

The issue is that custom tags don't seem to initialise in time while being called inside a content loop (your blocks content), causing the key part to return null inside the check tag method. If you move {{ partial:greeting }} to the top of content-blocks.html (above blocks), you'll see it suddenly works as expected.

Assuming this greeting hero is unique to the Homepage, I suggest you extract this into normal content (not nested as part of a replicator field).

Will be sure to close this issue once fixed, so will keep open for the time being.

@aryehraber aryehraber added the bug Something isn't working label Jan 14, 2020
@robhuska
Copy link
Author

Wow! Thanks for digging that one out. I will be able to implement your solution for now. Initially it is for the homepage, but would eventually like the ability to use cookies elsewhere. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants