4

The docs state that {{ craft.request.getCookie(name) }} will not work for cookies that were set in JavaScript, because all cookies in Craft go through some validation to ensure that they aren't tampered with.

This is understandable, but is there any way of getting non-Craft cookies with twig?

David Oliver
  • 223
  • 1
  • 9
Ben Croker
  • 7,341
  • 26
  • 55

2 Answers2

7

So far the only solution I have found is to use a plugin, and Lewis Jenkins' LJ Cookies plugin does the trick nicely:

{{ craft.lj_cookies.get( NAME ) }}

It also allows you to set and delete cookies within templates.

Ben Croker
  • 7,341
  • 26
  • 55
  • 1
    It really bugs me that I have to have a whole plugin just to access a cookie set from JS. That's a very "Wordpress" solution to a problem. The Craft guys need to figure out a correct way to do this. Given how ajax heavy most sites are it's a super common way to set cookies. Not much good if you can't get the values back. – JamesNZ May 02 '17 at 22:05
  • Please update the LJ Cookies Plugin Github Repo to https://github.com/khamer/craft-lj-cookies – Tunji Oyeniran Mar 28 '20 at 05:15
  • Updated, thanks! – Ben Croker Mar 28 '20 at 18:12
1

For now, you have to use a plugin

I recommend one of these 2

  1. Craft Cookies Plugin https://github.com/nystudio107/craft-cookies
  2. Craft LJ Cookies Plugin https://github.com/khamer/craft-lj-cookies