-
Notifications
You must be signed in to change notification settings - Fork 20
Saving user preferences with jQuery & Stash
Mark Croxton edited this page Feb 7, 2014
·
1 revision
Create a template such as site/save_preference
with the following code - adjust the regex to suit your needs:
{exp:switchee variable="{segment_3}" parse="inward"}
{!-- allow an alphanumeric name (and - or _) for the preference key only --}
{case value="#^[a-zA-Z0-9-_]+$#"}
{!-- allow a numeric value only --}
{exp:stash:set name="{segment_3}" type="snippet" save="yes" scope="user" match="#^[0-9]+$#"}{segment_4}{/exp:stash:set}
{/case}
{/exp:switchee}
Save a preference from jQuery - pass the variable name as segment_3
and the value in segment_4
:
$.ajax({ url: "/site/save_preference/my_variable/1"});
If you want to use it in your template code:
{!-- load variable into memory --}
{exp:stash:get name="my_variable" scope="user" type="snippet" default="0" output="no"}
{!-- use in a conditional --}
{if my_variable}
...
{/if}
Getting started
Using Stash
Using Mustash
- Mustash
- Installing Mustash
- Managing variables
- Managing bundles
- Cache-breaking rules
- Mustash plugins
- Mustash Varnish plugin
- Mustash plugin development
- Mustash API
Template design patterns
Tag reference
- {exp:stash:set}
- {exp:stash:get}
- {exp:stash:block}
- {exp:stash:set_value}
- {exp:stash:append}
- {exp:stash:append_value}
- {exp:stash:prepend}
- {exp:stash:prepend_value}
- {exp:stash:copy}
- {exp:stash:context}
- {exp:stash:is_empty}
- {exp:stash:not_empty}
- {exp:stash:set_list}
- {exp:stash:get_list}
- {exp:stash:append_list}
- {exp:stash:prepend_list}
- {exp:stash:split_list}
- {exp:stash:join_lists}
- {exp:stash:list_count}
- {exp:stash:unset}
- {exp:stash:flush_cache}
- {exp:stash:bundle}
- {stash:embed}
- {exp:stash:extend}
- {exp:stash:parse}
- {exp:stash:cache}
- {exp:stash:static}
- {exp:stash:finish}
- {exp:stash:not_found}
- Short tag syntax
- Using Stash methods in your own add-ons