zoukankan      html  css  js  c++  java
  • jinja2 使用教程

    jinja2模板使用教程

    网上很多教程都是直接copy官方文档。。。

    jinja2的内置测试器的行为非常迷惑。。。iterable,mapping,string等的测试行为实际上跟python中的理解不同:

    sw-office-lon-01 is an iterable: True
    sw-office-lon-01 is a sequence: True
    sw-office-lon-01 is a string: True
    
    4.22 is a number: True
    4.22 is an integer: False
    4.22 is a float: True
    
    65001 is a number: True
    65001 is an integer: True
    65001 is a float: False
    
    {'Ethernet1': 'Uplink to core'} is an iterable: True
    {'Ethernet1': 'Uplink to core'} is a sequence: True
    {'Ethernet1': 'Uplink to core'} is a mapping: True
    
    ['1.1.1.1', '8.8.4.4', '8.8.8.8'] is an iterable: True
    ['1.1.1.1', '8.8.4.4', '8.8.8.8'] is a sequence: True
    ['1.1.1.1', '8.8.4.4', '8.8.8.8'] is a mapping: False
    

    最后结论如下:

    So what all of this means? Well, I suggest the following tests for each type of variable:

    Number, Float, Integer - these work just as expected, so choose whatever fits your use case.

    Strings - it's enough to use string test:

    {{ my_string is string }}

    Dictionary - using mapping test is sufficient:
    {{ my_dict is mapping }}

    Lists - this is a tough one, full check should tests if variable is a sequence but at the same time it cannot be a mapping or a string:
    {{ my_list is sequence and my list is not mapping and my list is not string }}

    In some cases we know dictionary, or a string, is unlikely to appear so we can shorten the check by getting rid of mapping or string test:

    {{ my_list is sequence and my list is not string }}
    {{ my_list is sequence and my list is not mapping }}

    For the full list of available tests follow the link in References.

    以上部分来自顶部的链接。

  • 相关阅读:
    20200323 Go语言基础
    20200313 图表工具与redis使用
    20200312 CMDB的磁盘数据查询
    20200311 CMDB的表设计
    20200320 代码发布之完结
    20200319 代码发布之任务发布钩子脚本
    Ubuntu 安装 MySQL 服务
    使用U盘重装系统(删除掉系统自带的Windows 10)
    Linux & Windows 上安装 Qt
    初次使用Tampermonkey脚本管理器
  • 原文地址:https://www.cnblogs.com/LuoboLiam/p/15098288.html
Copyright © 2011-2022 走看看