Cool, but if this is a bug? 在 2019年1月3日星期四 UTC+8下午11:12:45,Juan Pablo Scaletti写道: > > As you can read on the documentation, no, you can't assign a value in a > loop and use it outside the loop, unless you use a namespace: > > http://jinja.pocoo.org/docs/2.10/templates/#assignments (see: Scoping > Behavior) > > > On Thu, Jan 3, 2019 at 3:30 AM Simon Jones <batma...@gmail.com > <javascript:>> wrote: > >> Hi all, >> >> I'm using Jinja2 template to generate config files, but I found a BUG, >> which is I could not assign value in a loop, like this: >> >> 1) use this template, could generate 'uuusevrf' >> >> {% block vrf_mgmt_interface %} >> {% set usevrf = 'no' %} >> {% set usevrf = 'nono' %} >> {% if MGMT_INTERFACE %} >> {% set uuusevrf = 'yes' %} >> {% for (name, prefix) in MGMT_INTERFACE %} >> {% if name == 'eth0' %} >> {% if MGMT_INTERFACE[(name, prefix)].has_key('vrfname') %} >> {% if prefix | ipv4 %} >> USE_VRF=2 >> IP_VERSION=4 >> ETH_NAME=eth0 >> ETH_IP={{ prefix | ip }} >> ETH_SUB_NET={{ prefix | network }}/{{ prefix | prefixlen }} >> ETH_LOOKUP_IP={{ prefix | ip }} >> VRFNAME={{ MGMT_INTERFACE[(name, prefix)]['vrfname'] }} >> GWADDR={{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }} >> {% else %} >> USE_VRF=2 >> IP_VERSION=6 >> ETH_NAME=eth0 >> ETH_IP={{ prefix | ip }} >> ETH_SUB_NET={{ prefix | network }}/{{ prefix | prefixlen }} >> ETH_LOOKUP_IP={{ prefix | ip }} >> VRFNAME={{ MGMT_INTERFACE[(name, prefix)]['vrfname'] }} >> GWADDR={{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }} >> {% endif %} >> {% else %} >> USE_VRF=1 >> {% endif %} >> {% endif %} >> {% endfor %} >> {% endif %} >> {{ usevrf }} >> {{ uuusevrf }} >> {% endblock vrf_mgmt_interface %} >> >> >> 2) could generate 'uuusevrf', this is result: >> >> rfaces.j211:~# sonic-cfggen -d -t >> /usr/share/sonic/templates/vrf_management_inter >> USE_VRF=2 >> IP_VERSION=4 >> ETH_NAME=eth0 >> ETH_IP=172.18.8.211 >> ETH_SUB_NET=172.18.8.0/24 >> ETH_LOOKUP_IP=172.18.8.211 >> VRFNAME=mgmtvrf >> GWADDR=172.18.8.1 >> nono >> yes >> >> >> 2) use this template, could NOT generate 'uuusevrf' >> >> {% block vrf_mgmt_interface %} >> {% set usevrf = 'no' %} >> {% set usevrf = 'nono' %} >> {% if MGMT_INTERFACE %} >> {% for (name, prefix) in MGMT_INTERFACE %} >> {% set uuusevrf = 'yes' %} >> {% if name == 'eth0' %} >> {% if MGMT_INTERFACE[(name, prefix)].has_key('vrfname') %} >> {% if prefix | ipv4 %} >> USE_VRF=2 >> IP_VERSION=4 >> ETH_NAME=eth0 >> ETH_IP={{ prefix | ip }} >> ETH_SUB_NET={{ prefix | network }}/{{ prefix | prefixlen }} >> ETH_LOOKUP_IP={{ prefix | ip }} >> VRFNAME={{ MGMT_INTERFACE[(name, prefix)]['vrfname'] }} >> GWADDR={{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }} >> {% else %} >> USE_VRF=2 >> IP_VERSION=6 >> ETH_NAME=eth0 >> ETH_IP={{ prefix | ip }} >> ETH_SUB_NET={{ prefix | network }}/{{ prefix | prefixlen }} >> ETH_LOOKUP_IP={{ prefix | ip }} >> VRFNAME={{ MGMT_INTERFACE[(name, prefix)]['vrfname'] }} >> GWADDR={{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }} >> {% endif %} >> {% else %} >> USE_VRF=1 >> {% endif %} >> {% endif %} >> {% endfor %} >> {% endif %} >> {{ usevrf }} >> {{ uuusevrf }} >> {% endblock vrf_mgmt_interface %} >> >> 4) could NOT generate, this is result: >> >> rfaces.j211:~# sonic-cfggen -d -t >> /usr/share/sonic/templates/vrf_management_inter >> USE_VRF=2 >> IP_VERSION=4 >> ETH_NAME=eth0 >> ETH_IP=172.18.8.211 >> ETH_SUB_NET=172.18.8.0/24 >> ETH_LOOKUP_IP=172.18.8.211 >> VRFNAME=mgmtvrf >> GWADDR=172.18.8.1 >> nono >> >> So, why ??? >> >> You could see that, same data, just the position of this line ({% set >> uuusevrf = 'yes' %}) is different. And the result show it definitely go >> into loop, but why this line NOT work ??? >> >> BTW, It's hard to debug !!!!!! >> >> -- >> You received this message because you are subscribed to the Google Groups >> "pocoo-libs" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to pocoo-libs+...@googlegroups.com <javascript:>. >> To post to this group, send email to pocoo...@googlegroups.com >> <javascript:>. >> Visit this group at https://groups.google.com/group/pocoo-libs. >> For more options, visit https://groups.google.com/d/optout. >> > > > -- > > Juan Pablo Scaletti >
-- You received this message because you are subscribed to the Google Groups "pocoo-libs" group. To unsubscribe from this group and stop receiving emails from it, send an email to pocoo-libs+unsubscr...@googlegroups.com. To post to this group, send email to pocoo-libs@googlegroups.com. Visit this group at https://groups.google.com/group/pocoo-libs. For more options, visit https://groups.google.com/d/optout.