我想创建一个宏,它需要跟踪以前是否被调用过。因此,我必须在宏的请求上下文中设置一个变量,但我不知道如何做到这一点。就像是:
{% macro my_macro() -%}
{% set g.foo = "bar" %} <-- Error
{{ g.get('foo') }}
{%- endmacro %}
Run Code Online (Sandbox Code Playgroud)
上述结果导致jinja2.exceptions.TemplateRuntimeError: cannot assign attribute on non-namespace object错误。
我知道这可能是对宏观概念的滥用,并且也适用于其他解决方案。我想跟踪宏的使用,所以我最多可以输出一些宏所需的 javascript,或者如果没有使用宏,则根本不输出。
当收件箱按钮被点击它运行inbox_open()函数和在收件箱中头3个按钮出现,但onclick事件侦听器是丢失。// inbox.open评论后检查代码,关键行:b.onclick = function() { console.log("button was clicked"); }和inbox.setAttribute("title", poH.innerHTML);
// inbox
var inbox = document.getElementById("inbox");
var inbox_news = document.getElementById("inbox_news");
var poH = document.createElement("span");
var poC = document.createElement("span");
var new_from_to = [0, 2, 3];
var number_of_news = [2, 1, 2];
var news_price_data = [10, 20, 30, 40, 50];
// inbox.open
function inbox_open() {
for (var i = 0; i < number_of_news.length; i++) {
var b = document.createElement("button");
b.innerHTML = (i + …Run Code Online (Sandbox Code Playgroud)