First of all I'm pretty new to Twig. I'm using a template system so that users can set some options to customize a template. Something similar like Shopify.
I was wondering if it is possible to grab all the settings and send them to javascript function to handle it further.
Let say a user can set these options:
{{ theme.hide_label }} // option to show/hide a label
{{ theme.label_color }} // option to set a color for the label
I could do something like this and then grab those variables and use them in a js function:
var hideLabel = '{{ theme.hide_label }}'; //true or false
var labelColor = '{{ theme.label_color }}'; // #000000
Unfortunatly I have a lot of settings, so this will be a pretty long list.
I've read about json_encode. But how can I group all those settings/options into something usable for a function?
Something like this:
var themeFunctions = {{ theme.label; theme.hidelabel; | t_json | raw }}
I've seen somebody did this for translating a lot of text with Twig:
var translations = {{ 'Add; Wishlist; Information; Add to wishlist;' | t_json | raw }};
And then created a function like so:
function getAjaxTranslation(key) {
var translation;
if (translation = eval('translations["' + key + '"]')) {
return translation;
}
return key;
}
Can something similar be done with variables that are not plain text?