Insert Jinja2 variables in Javascript

I am trying to reorganize my JS. I have various Morris.js functions inside <script>in my html file. This html is processed by a jar. Data from functions is transmitted directly from the server through the Jinja2 template system.

Is there a way to have these functions in a JS file and give them jinja values?

I really do not want to have onClick and JS scripts in html, I want to have all JS in separate JS files, if possible. As far as I know, I can’t, because these variables are passed by the engine, but, in my opinion, the script that imports the JS file is also displayed by the jinja engine, so I'm not sure.

I think that passing data through a tag metamay be possible, but I really do not want any hacks, if I cannot or do not have the β€œright” way to do this, do it.

Function example:

foo = function(){
        new Morris.Line({
              element: 'network-chart',
              data: [
              {% for load in node_statistics %}
                { y: {{load['time']}}000, a: {{load['b_sum_up']}}, b: {{load['b_sum_down']}}, c: {{load['b_max_up']}}, d: {{load['b_max_down']}} },

             {% endfor %}
              ],
              xkey: 'y',
              ykeys: ['a', 'b', 'c', 'd'],
              labels: ['sum up bandwidth', 'sum down bandwidth', 'max up bandwidth', 'max down bandwidth']
            });
    }

Thanks in advance.

+4
source share
1 answer

I think you can just pass arguments to your function if you put your js in a separate file.

0
source

Source: https://habr.com/ru/post/1662274/


All Articles