Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

hi, How to use cookie in volt??

hi, How to use cookie in volt??

I use {{ cookie.username }} in volt template, but nothing output.

Thanks.



7.8k

i'm not sure how to use cookie in volt but you can use it with DI

Register cookie in DI can only use in PHP codeļ¼Œ can't use in volt template.



62.7k
Accepted
answer
edited Nov '16

In volt you have to do:

{{ cookies.get('username').getValue() }}

// UPDATE: this also works
{{ cookies.get('username') }}

Check the example in the docs.

edited Nov '16

By default, Volt engine has access to IoC (i.e. services.php), which means that one can access any service object by it's name. So if you have a cookie service defined (or not defined, if you use Factory Default) you have access to it.