Call JavaScript function in jinja for loop [duplicate]

安稳与你 提交于 2021-02-10 05:12:44

问题


I have an HTML page, In the variable schedule has sequential decimal number in seconds.

My purpose is create a function to convert all these numbers in time using JavaScript/jQuery, but i could not understand, how can I invoke my function to convert all items?

<html>
    <body>
        // Jinja code

        {% for item in schedule %}

        {{ convertDecimal_to_time(item.someDecimal) }}

        {% endfor %}

    </body>
</html>

<script>
    covertdecimal_to_time(input_number){
        .....
        return time;
    }
</script>

回答1:


The Jinja code runs on your server. The Javascript runs on the client's browser.

You can't call a javascript function in a Jinja for-loop, because those two things happen at completely different times, on different machines.

The best approach for this scenario is to write a Python function, not a Javascript function, and run it as a filter. You can add a custom filter to the template engine.



来源:https://stackoverflow.com/questions/41188682/call-javascript-function-in-jinja-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!