使用Jinja2

时间:2018-09-18 22:20:39

标签: python templates jinja2

我正在尝试实现某种实用程序,该实用程序会生成某些模板的所有变体(组合)。例如:

{% all_combinations %}
     {% one_of('Hello', 'Welcome') %}, {% one_of('Jack', 'Alex') %}!
{% endall_combinations %}

应输出以下内容:

Hello, Jack
Hello, Alex
Welcome, Jack
Welcome, Alex

我为此目标选择了Jinja2。您能建议我如何实施吗?或建议我其他解决方案。

1 个答案:

答案 0 :(得分:1)

尝试一下,

from jinja2 import Template
t = Template("{{ greeting }}, {{ someone }}")

greetings = ('Hello','Welcome')
someones = ('Jack', 'Alex')

for g in greetings:
   for s in someones:
     print(t.render(greeting=g, someone=s))