1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add a few Jinja2 tests to simplify template (#37514)

Add list test to simplify template
This commit is contained in:
Dag Wieers 2018-03-20 23:08:55 +01:00 committed by GitHub
parent b7aa41b9b9
commit b8e07f0d6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 10 deletions

View file

@ -29,7 +29,7 @@ import os
import re import re
import sys import sys
import warnings import warnings
from collections import defaultdict from collections import defaultdict, Sequence
from distutils.version import LooseVersion from distutils.version import LooseVersion
from pprint import PrettyPrinter from pprint import PrettyPrinter
@ -132,6 +132,12 @@ def rst_xline(width, char="="):
return char * width return char * width
# NOTE: The existing Jinja2 'sequence' tests matches strings and dictionaries
def test_list(value):
''' Return true if objects is a list or tupple '''
return isinstance(value, Sequence)
def write_data(text, output_dir, outputname, module=None): def write_data(text, output_dir, outputname, module=None):
''' dumps module output to a file or the screen, as requested ''' ''' dumps module output to a file or the screen, as requested '''
@ -304,6 +310,9 @@ def jinja2_environment(template_dir, typ, plugin_type):
env.filters['html_ify'] = html_ify env.filters['html_ify'] = html_ify
env.filters['fmt'] = rst_fmt env.filters['fmt'] = rst_fmt
env.filters['xline'] = rst_xline env.filters['xline'] = rst_xline
env.tests['list'] = test_list
templates['plugin'] = env.get_template('plugin.rst.j2') templates['plugin'] = env.get_template('plugin.rst.j2')
if plugin_type == 'module': if plugin_type == 'module':

View file

@ -108,12 +108,10 @@ Parameters
<td> <td>
<div class="cell-border"> <div class="cell-border">
{# Turn boolean values in 'yes' and 'no' values #} {# Turn boolean values in 'yes' and 'no' values #}
{% if value.default is defined %} {% if value.default is sameas true %}
{% if value.default == true %} {% set _x = value.update({'default': 'yes'}) %}
{% set _x = value.update({'default': 'yes'}) %} {% elif value.default is sameas false %}
{% elif value.default == false %} {% set _x = value.update({'default': 'no'}) %}
{% set _x = value.update({'default': 'no'}) %}
{% endif %}
{% endif %} {% endif %}
{% if value.type == 'bool' %} {% if value.type == 'bool' %}
{% set _x = value.update({'choices': ['no', 'yes']}) %} {% set _x = value.update({'choices': ['no', 'yes']}) %}
@ -123,12 +121,12 @@ Parameters
<ul><b>Choices:</b> <ul><b>Choices:</b>
{% for choice in value.choices %} {% for choice in value.choices %}
{# Turn boolean values in 'yes' and 'no' values #} {# Turn boolean values in 'yes' and 'no' values #}
{% if choice == true %} {% if choice is sameas true %}
{% set choice = 'yes' %} {% set choice = 'yes' %}
{% elif choice == false %} {% elif choice is sameas false %}
{% set choice = 'no' %} {% set choice = 'no' %}
{% endif %} {% endif %}
{% if (value.default is string and value.default == choice) or (value.default is iterable and value.default is not string and choice in value.default) %} {% if (value.default is string and value.default == choice) or (value.default is list and choice in value.default) %}
<li><div style="color: blue"><b>@{ choice | escape }@</b>&nbsp;&larr;</div></li> <li><div style="color: blue"><b>@{ choice | escape }@</b>&nbsp;&larr;</div></li>
{% else %} {% else %}
<li>@{ choice | escape }@</li> <li>@{ choice | escape }@</li>