Home > Software design >  How to assign javascript array as jinja variable?
How to assign javascript array as jinja variable?

Time:10-05

I am trying to assign javascript array as jinja variable but it throws error

this is what it shows

Python code

  • i want the data to be taken as a array

CodePudding user response:

You can simply pass your list to the template. You can do without the conversion to JSON here.

return render_template('game.html', game=game, board=board)

To then use this variable in javascript, you can use the Jinja2 filter tojson. The result is your list as an array.

window.onload = start({{ board | tojson }});

CodePudding user response:

I haven't used json.dumps before but normally I can grab my variables I send in my render_template using this syntax.

var board_arr = $("#board").val();
window.onload = start(board_arr);
  • Related