1

On a site I'm working on, I've got a lists of projects. These lists have two views: a thumbnail and a text-list view. On each page, there's a button to switch to the other view.

I'd like to make that selection persistent. So hit the button to change views, and all pages load that view (until you change the view again).

Is there an easy/recommended way to do this?

Edit: would be nice if it were compatible with some sort of template caching.

1

Yes do it by setting cookie and then checking for the presence/value of the cookie and use the result to decide which layout to show (perhaps by using a conditional to use a particular embedded template).

As ever there are add-ons to help with getting and setting cookies, take a look at Cookies Plus as a starting point.

| improve this answer | |
  • I set up Cookies Plus after I'd posted this question - I can get it to read a cookie. But I can't figure out how to get cookies plus to write a cookie by using a button - i.e. press button, set cookie, reload page with new view. – SamC May 13 '14 at 16:44
  • From memory when I've done this I create a tiny template that has the Cookie Plus cookie setting tag and some kind of redirect (e.g. back to where the user came from). So when the button is clicked it sends the user to that template, sets the cookie and goes back to where they were. Maybe even use Ajax (not sure if you can set a cookie that way though). – foamcow May 13 '14 at 17:04
  • Ah. In my initial build, I had the cookie set by a different page, but I felt that was a weird behavior and that I was 'doing it wrong.' I've got it working now, more or less. I grabbed a jquery script that writes a cookie on my button click (github.com/carhartl/jquery-cookie) - a little clunky, but it's functional. Not in love with this solution, so I may regroup and try a different tack with Stash. – SamC May 13 '14 at 18:37
1

Cookie Plus is fine, but if you happen to already be using Stash, it can handle this kind of thing using save="yes" scope="user" parameters (which essentially creates a cookie).

https://github.com/croxton/Stash/wiki/%7Bexp%3Astash%3Aset%7D#scope--user

| improve this answer | |
  • I do use Stash for some other things, but haven't delved deep into all its functionality. And Stash is potentially friendlier with caching plugins. – SamC May 13 '14 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.