zip(list1,list2)在Jinja2中? [英] zip(list1, list2) in Jinja2?

查看:111
本文介绍了zip(list1,list2)在Jinja2中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Jinja2中进行代码生成,并且我经常想一起遍历两个列表(即变量名称和类型),有没有简单的方法可以做到这一点,或者我只需要传递一个预先压缩的列表?我在 docs 或谷歌搜索中找不到这样的功能。

I'm doing code generation in Jinja2 and I frequently want to iterate through two lists together (i.e. variables names and types), is there a simple way to do this or do I need to just pass a pre-zipped list? I was unable to find such a function in the docs or googling.

推荐答案

修改jinja2.Environment

Modify the jinja2.Environment global namespace itself if you see fit.

import jinja2
env = jinja2.Environment()
env.globals.update(zip=zip)
# use env to load template(s)

这可能有助于将视图(模板)逻辑与应用程序逻辑分离,但它也可以实现反向操作。 #关注点分离

This may be helpful in separating view (template) logic from application logic, but it enables the reverse as well. #separationofconcerns

这篇关于zip(list1,list2)在Jinja2中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆