使用jinja2的强大功能呈现模板文件。

Templer的Python项目详细描述


Templer-使用Jinja2制作模板

这是一个python 3模块,用于使用Jinja2呈现模板文件。变量的源是环境和上下文文件。

jinja2语法的文档可以在here中找到。

功能:

  • 使用Jinja2制作模板
  • 通过环境变量和上下文文件提供变量
  • 对上下文文件使用yaml
  • 默认值的简单定义
  • 动态上下文文件(渲染上下文文件本身)

目录


安装

直接从github安装:

pip install git+https://github.com/Aisbergg/python-templer@v1.1.4

从pypi安装:

pip install Templer

用法

usage: templer [-c CONTEXTFILE] [-d] [-r] [-f] [-h] [-i] [-m FILE_MODE] [-t]
               [-v] [--version]
               template [template ...] destination

Render template files with the power of Jinja2

positional arguments:
  template              File to be rendered. Path can be either a file or
                        directory containing multiple files (*.j2)
  destination           Destination for the rendered file(s)

optional arguments:
  -c CONTEXTFILE, --contextfile CONTEXTFILE
                        Context file to be used for rendering. Path can be
                        either a file or directory containing multiple files
                        (*.yml). The option can be used multiple times to
                        specify multiple paths
  -d, --dynamic-contextfiles
                        Render the context files like the templates before
                        parsing them
  -r, --remove-templates
                        Delete the templates after rendering
  -f, --force           Overwrite existing files
  -h, --help            Show this help message and exit
  -i, --ignore-undefined-variables
                        Ignore undefined variables
  -m FILE_MODE, --mode FILE_MODE
                        File mode for rendered files
  -t, --defaults-type-check
                        Check if the environment variables match the data type
                        of the defaults specified in a context file
  -v, --verbose         Enable verbose mode (-vv for debug mode)
  --version             Prints the program version and quits

有两个用于呈现模板的变量源。第一个源是系统环境,第二个源是上下文文件。

环境变量

当env被定义为FOO=BAR时,它可以用作模板文件中的{{ FOO }}{{ env.FOO }}

上下文文件

上下文文件的目的是作为默认变量的主要源,而环境变量将动态添加到渲染中。因此,上下文文件可以提供通用的默认配置和用于自定义它的环境变量。

上下文文件是用人类可读的yaml编写的。如果需要,还可以在分析上下文文件之前使用环境变量和jinja2呈现它们。

下面是一个详细的示例,其中说明了所有功能:

# Using the YAML syntax the definition of static variables is simple. Following# lines create three different variables that can be used in the templates like# `{{ static.var1 }}`.static:var1:"foo"var2:1var3:True# When the option `--dynamic-contextfiles` is supplied, then the context will be# rendered with Jinja2 using the environment variables before parsing its# content.dynamic:var4:"{{VAR4|default('bar')}}"var5:{{1.0 if VAR5 == 'True' else 2.0}}# A handy shortcut for defining defaults for variables is the `defaults`# section. `defaults` is a mapping where every key represents a default for a# variable. The format is simply: `VARIABLE: DEFAULT_VALUE`# Thus the environment variables will be checked if a variable with the name# `VARIABLE` is defined and if not it will be declared and set to the value# `DEFAULT_VALUE`. Templer takes the data type of the `DEFAULT_VALUE` into# account and tries to parse the given environment variable accordingly.# When the option `--defaults-type-check` is supplied, a failure in parsing the# right data type will result in an error and program termination.defaults:# type stringVAR6:"somestring"# type boolVAR7:False# type intVAR8:1# type floatVAR9:3.0# type list (env must be specified in json format like: ["a", "b", "c"])VAR10:-"foo"-"bar"-"uff"# ----------------------------------------------------------------------------# To simplify some more use cases there are a couple of special defaults# available:# Type: Choice# This type checks a user choice against a list of possible choices. If the# given choice is not in the list, then an error is thrown. In the following# example a given env `VAR11=c` is considered a valid choice. A value like# `VAR11=z` is considered invalid and therefore the program will terminate.VAR11:type:choice# type of special defaultsdefault:b# default choicecase_sensitive:False  # optional (default:false)strip:True            # optional (default:true)choices:# list of available choices-a-b-c# Type: List# This type extends the simple 'list' default, so lists don't have to be# defined as json list. Instead a delimiter can be defined to split up a# string into a list:VAR12:type:listdelimiter:","# delimiter for splitting the stringstrip:True            # optional (default:true)default:# default list to use-a-b-c# Type: Variation# This type adds different variations of default values. In the following# example there are three variation (`SMALL`, `MEDIUM`, `LARGE`) defined. A# variation is used when the related environment variable is defined. So for# example if `SMALL=''` is defined then the variables `VAR13: 1` and# `VAR14: "a"` will be used.SMALL:type:variationdefaults:# can be same structure as the main `defaults`VAR13:1VAR14:"a"MEDIUM:type:variationdefaults:VAR13:10VAR14:"aaa"LARGE:type:variationdefaults:VAR13:100VAR14:"aaaaaaaaa"

模板

使用jinja2使用上下文文件和环境中定义的变量呈现模板。因此,可以使用任何特定于jinja2的语法。

通过提供指向多个文件的路径或指向可能包含多个模板文件的目录的路径,可以在一个模板上呈现多个模板。提供目录路径后,将搜索扩展名为.j2.jinja2的文件。当保留目录结构时,这些将被呈现并放在目标路径下。

示例

仅使用环境变量呈现单个模板文件(examples/1):

NOUN=fool templer -f template.ini.j2 rendered.ini

使用上下文文件呈现多个模板(examples/2):

templer -f -c context.yml templates/ rendered/

Context File sectionexamples/3)中描述的全功能示例:

VAR4=ui VAR5=True VAR8=3VAR11=b VAR12="1,2,3"LARGE=0 templer -d -f -c context.yml template.ini.j2 rendered.ini

生产中使用的示例(examples/4):

exportNGINX_TLS_CERT=''exportNGINX_TLS_KEY=''exportPHPMYADMIN_BLOWFISH_SECRET="$( </dev/urandom tr -dc '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%'| head -c40;echo"")"
templer -d -f -t -c vars/ templates/ rendered/

正在运行的mandatory筛选器(examples/5):

templer -d -c context.yml -f template.ini.j2 rendered.ini

在那些广泛使用templerDockerfiles中可以找到更多的实际例子。

额外的Jinja2过滤器

FilterDescription
^{}If the variable is not defined an error with a message ^{} will be thrown

许可证

Templer是根据lgpl v3许可证发布的。有关详细信息,请参见LICENSE.txt

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在Eclipse中使用多个调用在一行上打印java   javajackson序列化问题。只有同一实体的第一个对象可以很好地序列化   Java中Deflate函数的等价充气   使用customlitview的java Android actionbar搜索   java“<T>T get()”是什么意思?(它有用吗?)   目标c使用CommonCrypto使用AES256加密,使用OpenSSL或Java解密   java在运行时更新资源文件   fileinputstream在java中访问并将数据写入现有文件   带集群的java Android Mapbox我希望每个功能都有不同的标记图像   java JDK8>JDK10:PKIX路径生成失败:SunCertPathBuilderException:找不到请求目标的有效证书路径   java使用Hk2生成具有指定构造函数参数的实例   为什么这个系统。出来Java中的println()打印到控制台?   java目录和文件名连接不起作用   使用mockito和通配符绘图的java