在Jinja中调用JavaScript函数进行循环 [英] Call JavaScript function in jinja for loop
问题描述
我有一个HTML页面,在变量schedule
中具有以秒为单位的连续十进制数.
I have an HTML page, In the variable schedule
has sequential decimal number in seconds.
我的目的是创建一个函数,以使用JavaScript/jQuery及时转换所有这些数字, 但我不明白,如何调用我的函数来转换所有项目?
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>
推荐答案
Jinja代码在您的服务器上运行. Javascript在客户端的浏览器上运行.
The Jinja code runs on your server. The Javascript runs on the client's browser.
您不能在Jinja for循环中调用javascript函数,因为这两种情况在完全不同的时间发生在不同的机器上.
You can't call a javascript function in a Jinja for-loop, because those two things happen at completely different times, on different machines.
此方案的最佳方法是编写Python函数而不是Javascript函数,然后将其作为过滤器.您可以将自定义过滤器添加到模板引擎.
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.
这篇关于在Jinja中调用JavaScript函数进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!