8 公共函数

def ab(a,b):
return a*b

@app.template_global() #变成公共函数,每个页面都可以用
def aabb(a,b):
return a * b    #html页面中直接可以使用 {{ aabb(100,100) }} 函数直接计算返回结果


@app.template_filter() #待筛先功能的公共函数,每个页面都可以用
def axc(a,b):
return a + b    #html 页面直接可以使用 {{ aabb(100,100) |axc(1) }} 先计算aabb函数的结果等于10000 然后拿这10000 到当前函数 axc中处理 a=10000 b=1 相加结果等于1
    

@app.route("/jinja2",methods=['GET','POST'])
def jinja22():
return render_template("jinja2.html",func=ab) #func函数计算执行的上面的ab函数, 这时候 html 文件中需要传递{{ func(11,99) }} 就会返回页面结果11*99结果



原文地址:https://www.cnblogs.com/ajaxa/p/11156528.html