New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No way to test type #358
Comments
Do you really need to know the type? Are the tests like number and string not enough? http://jinja.pocoo.org/docs/templates/#list-of-builtin-tests I guess you could add type as a customer filter. But this really doesn't feel very useful.
output:
|
That can be easily added by creating a custom test and adding it to your template context. Afterwards you could e.g. use However, I'd say it's much cleaner if you expose the "type" of your object via an attribute. After all, they'll be somehow similar, maybe even of a common base class? Based on your form field example, |
Come here from google. Case: if 1 (one) address, then make string, else make array. string_address: '10.10.10.10/24'
list_adress:
- 10.10.10.10/24
- 10.10.20.20/24 {% if item.address is defined and item.address | ipaddr('host/prefix') %}
{% if item.address is string %}
Address=('{{ item.address }}')
{% elif item.address %}
Address=({% if item.address %}{{ '\'' + item.address | join('\' \'') + '\'' }}{% endif %})
{% endif %}
{% endif %} With jinja we can check type is string. It could be someone will be useful. |
One year later... Check for the data type can be made even more accurate (for example string or sequence). ---
network_scripts_interfaces:
- interface: 'vlan2'
address:
- '192.168.1.1/24'
- '192.168.2.1/24'
- interface: 'vlan3'
address: '192.168.3.1/24' {% if item.address is defined and item.address != '' %}
{% if item.address is string and item.address | ipaddr('host/prefix') %}
{{ 'IPADDR' + '=\"' + item.address + '\"' }}
{% elif item.address is sequence %}
{% for addr in item.address %}
{% if addr | ipaddr('host/prefix') %}
{{ 'IPADDR' ~ loop.index ~ '=\"' ~ addr ~ '\"' }}
{% endif %}
{% endfor %}
{% endif %}
{% endif %} |
This is might not work as expected, as string type will also be true if you test with
Therefore it would be really great to add strict type comparisons. So we could do |
We need a way to be able to test the type of an object is equal to one another.
An example use case is when trying to print a certain type of form element based on the type of a given object.
The text was updated successfully, but these errors were encountered: