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.

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

  • 相关阅读:
    七个高效的文本编辑习惯(以Vim为例)
    rbx1 package 下载安装过程
    ros机器人开发概述
    ROS BY EXAMPLE 1 -- 环境设置与安装
    除法取模练习(51nod 1119 & 1013 )
    kinect driver install (ubuntu 14.04 & ros-indigo)
    ros问题总结
    200行代码搞定炸金花游戏(PHP版)
    JavaScript方法call,apply,caller,callee,bind的使用详解及区别
    javascript中apply、call和bind的区别
  • 原文地址:https://www.cnblogs.com/LuoboLiam/p/15098288.html
Copyright © 2011-2022 走看看