Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set javascript value to jinja variable

I know we can set Jinja variable to js variable like this.

var x = {{ 'value' }}

but I am trying to do the reverse. i.e I am trying to set javascript value to jinja variable. I tried the following but nothing worked.

{{value}} = x
{% set value = x %}
{% set value %} x {% endset %}

x is javascript variable.

is there any way to achieve this.

like image 758
shakthydoss Avatar asked Apr 13 '17 04:04

shakthydoss


2 Answers

This is not possible, and reflects a deep problem in your understanding of the web application architecture. jinja2 is a templating engine for Python, running on your server. JavaScript you are talking about is running in your browser.

When a server receives a request, it processes a jinja2 template to obtain the response it will send to the client. You cannot "set Jinja variable to js variable", you can render the page in such a way that eventually a JavaScript variable would be assigned a value that was in a Jinja2 variable. I.e. you start with var x = {{ value }} in jinja2 template; jinja2 transforms it into var x = 42 during its rendering; then the rendered result gets sent to the client, which executes it as JavaScript, assigning a value 42 to x. The fact that there is a temporal sequence here (Python -> jinja2 -> rendered page source -> JavaScript) means you can't do what you want to do: The jinja2 variable and the JavaScript variable don't exist at the same time, nor in the same place, and assigning a JavaScript value to a jinja2 variable is impossible.

In order to send the value 42 to Python (and possibly later to jinja2), you would need to make a request that would send that value from client to server: a form submission, or an AJAX request.

You might get a better response if you would describe exactly what you want to do that you think requires you to "set javascript value to jinja variable".

like image 176
Amadan Avatar answered Oct 05 '22 23:10

Amadan


This is an old thread and the answer might still be "it cannot be done" but I would like to point out a mistake in both the answers above. The Question is "How to set javascript value to jinja variable". What He is asking is if you have JavaScript running on your front end page that needs a certain value to work and that value is given by the backend script and passed in via Jinja2 can you make that work? (AJAX perhaps).. Both "Answers" above while detailed and informative don't address this question. They both seem to address the opposite question, "Can you set a jinja2 variable from a javascript" which I agree does not make sense. I'm looking for the answer to the same question (How to set javascript value to jinja variable) because This tutorial on flask with charts shows you inserting a jinja variable into a java script. The problem is Java looks at the double bracket ( {{ VAR1 }} )as part of the script and throws errors.

like image 36
Michael Gallo Avatar answered Oct 06 '22 00:10

Michael Gallo