interpolate a Javascript variable as to assign it's value to a TT variable
More educational value out of this question than a practical one Is it possible to interpolate a Javascript variable as to assign it's value to a TT variable? The scenario would be that from within the materialized template to extract value pairs from DOM elements like sliders,select groups etc,serialize them and save the resulting string into a cookie: <script> var getvalue = $( "#Brand").find(":selected").parent("optgroup").attr("label"); jsvar = {Brand:getvalue}; jsvar = JSON.strignify(jsvar); [% SET session.ttvar= jsvar ]% </script> Imagine going through a series of elements,capturing the values which can be used later on as parameters to a POST call i.e {Brand:...,Price_from....,Price_to:...} since they would be saved in a cookie which would be valid for the rest of the session, they could be reused by the same or another route for purposes of for example reinstating or remembering the previous state after the back button of the browser is hit Of course, that could be done with an Ajax call but been thinking of alternative ways So the problem here is how do you escape out of the TT context and inject temporary Javascript context in [% SET session.ttvar= jsvar %] ,so that jsvar gets assigned to ttvar ?
On Wed, 2015-09-09 at 01:13 +0300, Nikos Vaggalis wrote:
Is it possible to interpolate a Javascript variable as to assign it's value to a TT variable?
No, because the TT variable has already been rendered to its value once the JS code runs.
The scenario would be that from within the materialized template to extract value pairs from DOM elements like sliders,select groups etc,serialize them and save the resulting string into a cookie:
<script> var getvalue = $( "#Brand").find(":selected").parent("optgroup").attr("label"); jsvar = {Brand:getvalue}; jsvar = JSON.strignify(jsvar); [% SET session.ttvar= jsvar ]%
^^ this would be rendered as blank when the JS runs
</script>
Imagine going through a series of elements,capturing the values which can be used later on as parameters to a POST call i.e
{Brand:...,Price_from....,Price_to:...}
since they would be saved in a cookie which would be valid for the rest of the session, they could be reused by the same or another route for purposes of for example reinstating or remembering the previous state after the back button of the browser is hit
Of course, that could be done with an Ajax call but been thinking of alternative ways
Yeah, if you want them in your Dancer application, then you'd have to somehow get the JS code to post them back into it, with an Ajax-type call. Andy
The issue is that the template work is done /server-side/ and the javascript is run /client-side/ so you cannot send client data to the server before it gets rendered unless you make another server request once the data is read. (But you can put server values into client vars.) However, you say "save the resulting string into a cookie". You can do that in javascript or jQuery. For the latter |$.cookie("cookiename",JSON.stringify($("#MyData").data()));| or in pure JavaScript document.cookie="cookiename=whatever; expires=Thu, 10 Sep 2015 00:00:01 UTC; path=/mypath"; On 9/8/2015 4:13 PM, Nikos Vaggalis wrote:
More educational value out of this question than a practical one
Is it possible to interpolate a Javascript variable as to assign it's value to a TT variable? The scenario would be that from within the materialized template to extract value pairs from DOM elements like sliders,select groups etc,serialize them and save the resulting string into a cookie:
<script> var getvalue = $( "#Brand").find(":selected").parent("optgroup").attr("label"); jsvar = {Brand:getvalue}; jsvar = JSON.strignify(jsvar); [% SET session.ttvar= jsvar ]% </script>
Imagine going through a series of elements,capturing the values which can be used later on as parameters to a POST call i.e
{Brand:...,Price_from....,Price_to:...}
since they would be saved in a cookie which would be valid for the rest of the session, they could be reused by the same or another route for purposes of for example reinstating or remembering the previous state after the back button of the browser is hit
Of course, that could be done with an Ajax call but been thinking of alternative ways
So the problem here is how do you escape out of the TT context and inject temporary Javascript context in [% SET session.ttvar= jsvar %] ,so that jsvar gets assigned to ttvar ?
-- John J. McDermott, CPLP Learning and Performance Consultant jjm at jkintl.com 575/737-8556 Check out my security blog posts <http://cybersecurity.learningtree.com> Add an A for the Arts To STEM and get STEAM and a strong engine to move forward.
participants (3)
-
Andrew Beverley -
John J. McDermott, CPLP -
Nikos Vaggalis