Calculation with a jinja type
Objectives
In this section we will learn how to create new ways of calculation.
Up to now, our only way of dynamically (that is, during the runtime) calculating a value is to point on another variable’s value. But this is not the only way.
Another jinja calculation type sample
We can now hide or disable some variables or families with other techniques than pointing on a variable’s value.
Let’s reason upon the proxy’s manual configuration we discussed before.
We have the dict/02-proxy_manual.yml
structure file:
structfile/02-proxy_manual.yml
file ---
version: '1.1'
manual:
description: Manual proxy configuration
type: family
disabled:
type: jinja
jinja: |
{% if rougail.proxy.proxy_mode != 'Manual proxy configuration' %}
the proxy mode is not manual
{% endif %}
Question
question: OK then. What happens when you select the “Manual proxy configuration”?
Here if the user selects the “Manual proxy configuration” proxy mode,
the the manual
family will be disabled. This is what the jinja code says.
Let’s explain it more precisely.
Note
The “the proxy mode is not manual” output is be used in the log outputs for example while
Why Jinja?
What about this Jinja
type?
If the Jinja template returns some text, then the family will be disabled
. Otherwise it is accessible.
Deactivating a family means that we will not be able to access it as well as the variables or families included in this family.
Note
If the Jinja template does not return any text, the variable will be enabled. Here we are using the Jinja condition statement.
- Jinja
Jinja is a template engine. we are using Jinja in a classical way, that is, Jinja allows us to handle different cases, for example with the
if
statement.
Todo
montrer aussi ici des exemples de calculs de valeurs variables, ce qui est un des usages principaux de jinja
Key points
Here we have come to the possibility of making any kind of calculations based on the state of the configuration. This is an important feature to manage the stateful aspect of a configuration.