XF 2.2 Templates: show content only if a cookie is set

euge

Member
Hello,
In the PAGE_CONTAINER template, I would like to show some custom content only if a particular cookie (let's call it "mycookie") is NOT set.
What's the easiest way? I have read some posts in the forum, including a post about the use of:

$xf.app.request.getCookie('mycookie')

but I don't know how to integrate this with the template code and to use it as conditional statement.

Thanks!
 
Maybe something like that?
HTML:
<xf:if is="$xf.app.request.getCookie('mycookie')">
   Show content...
</xf:if>

Edit, check this:
 
Last edited by a moderator:
Hi,
thanks for your answer.
It's just a technical cookie I set for myself, not third party so I don't think I need consent.

I need to show the content only when the cookie is NOT set, so I tried

Code:
<xf:if is="!$xf.app.request.getCookie('cookiename')">

but it doesn't seem to work. I am not sure if I have properly understood how this work, though: I guess
xf.app.request.getCookie('cookiename')
returns the cookie value and not TRUE|FALSE based on the existence of the cookie, correct?
If this is the case, is the condition evaluated TRUE when getCookie returns any value?

Thanks.
 
I also tried, to debug, to print out the value of the cookie

Code:
{{ $xf.app.request.getCookie('cookie_name') }}

but nothing is displayed. I can see the cookie is set from the browser console.
 
Make sure the cookie is prefixed appropriately (xf_ is the default cookie prefix). You can dump the value with:

HTML:
{{ dump($xf.app.request.getCookie('cookie_name')) }}
 
Hi,
I didn't know the cookie name must start with xf_, Where can I find documentation about this?

Anyway, I tried to set via PHP a cookie whose name starts with xf_, while browsing the forum I can see the cookie with value "1" in my browser console but if in the template I add

Code:
{{ dump($xf.app.request.getCookie('cookie_name')) }}

I get FALSE.

The cookie has path "/". This should be fine even if xenforo is under /forum but I also tried to change the path (using the browser console) to "/forum" and I still got FALSE.

Any clue?

thanks.
 
Back
Top Bottom