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.

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

  • 相关阅读:
    Nginx 部署多个 web 项目(虚拟主机)
    Nginx 配置文件
    Linux 安装 nginx
    Linux 安装 tomcat
    Linux 安装 Mysql 5.7.23
    Linux 安装 jdk8
    Linux 安装 lrzsz,使用 rz、sz 上传下载文件
    springMVC 拦截器
    spring 事务
    基于Aspectj 注解实现 spring AOP
  • 原文地址:https://www.cnblogs.com/LuoboLiam/p/15098288.html
Copyright © 2011-2022 走看看