XF 2.0 Set and get cookies

Lukas W.

Well-known member
What're the most optimal ways to get and set cookies in XF2?

I figured I can read cookies from the app with
Code:
$request = \XF::app()->container('request');

$cookies = $request->getCookies();
$specificCookie = $request->getCookie('someCookie');

And set them via the template renderer
Code:
$renderer->getResponse()->setCookie('name', 'value');

Any better ways or are they fine?
 
I have a question about this, hoping that someone can point me in the right direciton.
For an addon I am making, I want to get/set a cookie from within a template.
Where should I put the code that @Lukas W. mentioned?
 
$xf.app.request.getCookie('foo'), but I don't think you can set cookies from templates (without using JS) with vanilla XF 2.
 
In the end I used JS Cookie for this.
I also saw someone using template code like this:
Code:
<span id="collapse-{$node.node_id}" class="collapseTrigger collapseTrigger--block {{ !is_toggled('_node-' . $node.node_id) ? ' is-active' : '' }}" data-xf-click="toggle" data-xf-init="toggle-storage" data-storage-type="cookie" data-target=".block--category{$node.node_id} .block-body" data-storage-key="_node-{$node.node_id}"></span>
So I guess it should be possible to at least store something using template code.
 
This will store it locally and keep it across a logout session.

HTML:
<span id="collapse-{$node.node_id}" class="collapseTrigger collapseTrigger--block is-active" data-xf-click="toggle" data-xf-init="toggle-storage" data-target=".block--category{$node.node_id} .block-body" data-storage-key="_node-{$node.node_id}"></span>
 
You do not need a 3rd party lib to set cookies

Code:
<xf:js>XF.Cookie.set('name', 'value', new Date(new Date().setFullYear(new Date().getFullYear() + 1)));</xf:js>
I know this is an old post, but is it possible to do this on button click?
 
Back
Top Bottom