gqli
gqli

Reputation: 1055

Hide attribute definition if the value is empty in Jinja?

<vcpu placement='{{cpu.placement}}' cpuset='{{cpu.cpuset}}' current={{cpu.current}}>{{ cpu['maximum'] }}</vcpu>

Giving that we have 3 possible attributes and any of them can be empty.

Is there a neat way to hide the attribute definition if the value is empty? Let's say if cpu.placement is empty, the line placement='' shouldn't be there in the XML definition.

Upvotes: 0

Views: 592

Answers (1)

Dauros
Dauros

Reputation: 10557

Just put the attribute definition inside an if-block:

<vcpu 
  {% if cpu.placement %}placement='{{cpu.placement}}'{% endif %}
  {% if cpu.cpuset %}cpuset='{{cpu.cpuset}}'{% endif %}
  {% if cpu.current %}current={{cpu.current}}>{{ cpu['maximum'] }}{% endif %}
</vcpu>

See the list of builtin tests in you need more complicated test for a variable.

Upvotes: 1

Related Questions