Kalanamith
Kalanamith

Reputation: 20678

How to set a value to a Jinja2 variable by Java script

I am trying to populate an HTML table using a set of Jason Data Objects. In order to do this i am using jinja template. For data population purpose um going to iterate a jinja data list and i want to set the list value from a java script function .

HTML TEMPLATE:

{% for object in list %}
 <td>object.name</td>
 <td>object.age</td>
{% endfor %}

IN AJAX HANDLER Im Sending a list Via JSON and its in my Java script. How to set the list jinja2 variable value from Javascript or from Ajax Handler?

Upvotes: 0

Views: 4121

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599876

You should think about what you're asking here. Jinja2 is a server-side technology, and the template is rendered before it gets sent to the browser. Your Ajax function runs on the client side, after the list variable has been turned into HTML. There is no way to do what you're asking.

Either get your Ajax function to return rendered HTML rather than JSON (ie render the Jinja template in the Ajax server-side handler) or use a client-side template technology - the author of Jinja2, Armin Ronacher, has also written JsonJinja which might do what you want.

Upvotes: 10

Related Questions