简体   繁体   English

如何为嵌套和fors的python代码编写jinja模板?

[英]How to write jinja template for python code with nested for and ifs?

I have written a python file to render a jinja template from the json data 我已经写了一个python文件来从json数据渲染一个Jinja模板

import jinja2
from json import dumps

temploader = jinja2.FileSystemLoader(searchpath = '/')
tempenv = jinja2.Environment(loader=temploader,extensions=['jinja2.ext.loopcontrols'])
tempenv.filters['dumps'] = dumps

temp_file =  "/home/templates/myfile.tmpl"

temp = tempenv.get_template(temp_file)

ifconf = [
    {
    "Interface": "eth0", 
    "IP": "172.45.100.239", 
    "MAC": "48:4d:7e:55:4e:z8", 
    "Mask": "255.255.255.0", 
    "MTU": "1500"
   }, 
   {
    "Interface": "lo", 
    "IP": "127.0.0.1", 
    "MAC": "", 
    "Mask": "255.0.0.0", 
    "MTU": "65536"
   }, 
   {
    "Interface": "tun0", 
    "IP": "10.120.20.229", 
    "MAC": "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00", 
    "Mask": "255.255.255.255", 
    "MTU": "1400"
   }, 
  {
    "Interface": "vboxnet0", 
    "IP": "192.168.43.1", 
    "MAC": "0a:00:27:00:00:00", 
    "Mask": "255.255.255.0", 
    "MTU": "1500"
  }
 ]


inp = {"data":ifconf}
out = temp.render(inp)
print out

Now, My desired output should be like this 现在,我想要的输出应该是这样的

{'Interface': ['eth0', 'lo', 'tun0', 'vboxnet0'], 'IP': ['172.23.106.239', '127.0.0.1', '10.120.233.229', '192.168.33.1'], 'MAC': ['48:4d:7e:9e:4e:b8', '', '00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00', '0a:00:27:00:00:00'], 'Mask': ['255.255.255.0', '255.0.0.0', '255.255.255.255', '255.255.255.0'], 'MTU': ['1500', '65536', '1400', '1500']}

The jinja template file : myfile.tmpl Jinja模板文件:myfile.tmpl

{%- set di = dict.fromkeys(data[0],[]) -%}
{%- for i in data -%}
    {%- for k,v in di.items() -%}
        {%- for x,y in i.items() -%}
            {%- if x == k -%}
                 {%- set dummy = di[k].append(y)-%}
                 {%- break -%}
            {%- else -%}
                 {%- continue -%}
           {%- endif -%}          
       {%- endfor -%}
    {%- endfor -%}
{%- endfor -%}
{{di}}

I know break and continue doesn't work in jinja if I don't use loop control extensions. 我知道如果不使用循环控制扩展,在Jinja中中断和继续操作将不起作用。 Even after using loop control extension I'm not getting the desired output. 即使在使用循环控制扩展后,我也无法获得所需的输出。

Could you guys please help me: 你们能帮我吗:

  1. To correct the jinja template using loop control extensions to get desired output 使用循环控制扩展来纠正Jinja模板以获得所需的输出

  2. To get the desired output WITHOUT loop control extensions (since in my requirement is I should not be using loop control extensions.) 为了获得所需的输出而没有循环控制扩展(因为我的要求是我不应该使用循环控制扩展。)

Firstly, there's no reason to do this in the template. 首先,没有理由在模板中执行此操作。 Secondly, your logic could be simplified massively no matter where you do it: 其次,无论在何处进行操作,都可以大大简化您的逻辑:

keys = ifconf[0].keys()
data = {key: [val[key] for val in ifconf] for key in keys}

Try this : 尝试这个 :

dic = {}
for i in ifconf[0].keys():
    l = [j[i] for j in ifconf]
    dic[i] = l

print(dic)
{'IP': ['172.45.100.239', '127.0.0.1', '10.120.20.229', '192.168.43.1'],
 'Interface': ['eth0', 'lo', 'tun0', 'vboxnet0'],
 'MAC': ['48:4d:7e:55:4e:z8','','00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00',
  '0a:00:27:00:00:00'],
 'MTU': ['1500', '65536', '1400', '1500'],
 'Mask': ['255.255.255.0', '255.0.0.0', '255.255.255.255', '255.255.255.0']}

One liner: 一班轮:

>>> { i:[j[i] for j in ifconf] for i in ifconf[0].keys() }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM