简体   繁体   English

通过过滤渲染 jinja2 模板时包含和排除字符串 output

[英]Include and exclude string output when rendering jinja2 template via filtering

I'm trying to construct a template to support configuration lines for a specific console platform.我正在尝试构建一个模板来支持特定控制台平台的配置行。

The running-configuration is in this following format:运行配置采用以下格式:

Example 1:示例 1:

/settings/network_connections/ETH0/ipv4_mode=static
/settings/network_connections/ETH0/pv4_address=10.0.0.10
/settings/network_connections/ETH0/ipv4_bitmask=24 
/settings/network_connections/ETH0/ipv4_gateway=10.0.0.1

If you want to configure the following above lines, it's prepended by the word "set" like this:如果你想配置以下这些行,它会在前面加上“set”这个词,如下所示:

Example 2:示例 2:

set /settings/network_connections/ETH0/ipv4_mode=static
set /settings/network_connections/ETH0/pv4_address=10.0.0.10
set /settings/network_connections/ETH0/ipv4_bitmask=24 
set /settings/network_connections/ETH0/ipv4_gateway=10.0.0.1

In my jinja2 template, I have something like this:在我的 jinja2 模板中,我有这样的内容:

Example 3示例 3

{{ set| remediation }} /settings/network_connections/ETH0/ipv4_mode=static
{{ set| remediation }} /settings/network_connections/ETH0/pv4_address=10.0.0.10
{{ set| remediation }} /settings/network_connections/ETH0/ipv4_bitmask=24
{{ set| remediation }} /settings/network_connections/ETH0/ipv4_gateway=10.0.0.1

I want to be able to render the template and be able to output what I have in Example 2 (with 'set') as well as the ability to output as Example 1 (without 'set') using a boolean variable (with_remediation). I want to be able to render the template and be able to output what I have in Example 2 (with 'set') as well as the ability to output as Example 1 (without 'set') using a boolean variable (with_remediation). If True, include 'set' - else exclude 'set'.如果为真,则包括“设置” - 否则排除“设置”。 In Example 3, 'remediation' being a embedded custom filter.在示例 3 中,“补救”是嵌入式自定义过滤器。

import jinja2

loader = jinja2.FileSystemLoader('/tmp')
env = jinja2.Environment(autoescape=True, loader=loader)

def remediation(input,with_remediation):
    """Custom filter"""
    if(with_remediation):
        return input
    else:
        return ""

env.filters['remediation'] = remediation
temp = env.get_template('template.jinja2')
temp.render(set="set")

But I'm unsure of how to pass the variable 'with_remediation' into the function remediate.但我不确定如何将变量“with_remediation”传递给 function 修复。

I have tried following the example Embed custom filter definition into jinja2 template?我尝试按照示例将自定义过滤器定义嵌入 jinja2 模板? provided in the answer, but not sure if it will help in what I'm trying to achieve.在答案中提供,但不确定它是否有助于我想要实现的目标。

Also, how can I code it so 'set' can be any 'string' I want it to be?另外,我如何编码它以便“设置”可以是我想要的任何“字符串”? Must I include every string I want to use in the temp.render(set="set") line?我必须在temp.render(set="set")行中包含我想使用的每个字符串吗? For example;例如; temp.render(set="set", delete="delete",rename="rename") or if there a more efficient way of tackling this? temp.render(set="set", delete="delete",rename="rename")或者是否有更有效的方法来解决这个问题?

I was able to get resolve it by doing this;我可以通过这样做来解决它; output 'set' by setting with_remediation to True; output 通过将 with_remediation 设置为 True 来“设置”; exlude output 'set' by setting with_remediation to False:通过将 with_remediation 设置为 False 来排除 output 'set':

import jinja2

loader = jinja2.FileSystemLoader('/tmp')
env = jinja2.Environment(autoescape=True, loader=loader)

with_remediation = True

def remediation(input):
    """Custom filter"""
    if(with_remediation):
        return input
    else:
        return ''

env.filters['remediation'] = remediation
temp = env.get_template('template.jinja2')
temp.render(set='set')

Template:模板:

{{ set | remediation }}/settings/network_connections/ETH0/ipv4_mode=static
{{ set | remediation }}/settings/network_connections/ETH0/pv4_address=10.0.0.10
{{ set | remediation }}/settings/network_connections/ETH0/ipv4_bitmask=24
{{ set | remediation }}/settings/network_connections/ETH0/ipv4_gateway=10.0.0.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM