I have a checkbox im trying to build in Volt:
<input type="checkbox" class="myClass" data-size="small" data-type="{{ type.getType() }}">
So now i would normally write it like this
{{ check_field( 'class':'my class', 'data-size':'small', 'data-model-pk': ''~ AclGroup.id_group ) }}'
However i would like to do something like this:
<input type="checkbox" class="myClass" {% if AclGroup.flg_active == 1 %} checked="" {% endif %} data-size="small" data-type="{{ type.getType() }}">
But i have no idea how to do a statement inside {{ }}
I tried breaking out of the {{ }}{% %}{{ }}
and a bunch of other stuff but i cant find any documentation that covers it and nothing i tried works. Any ideas?
You could always leave it as you have given in your example - Volt is, at times, just a nice way to produce Html after all.
However, I would do this
{% if AclGroup.flg_acive == 1 %}
{{ check_field( 'class':'my class', 'checked': "", 'data-size':'small', 'data-type': type.getType() ) }}
{% else %}
{{ check_field( 'class':'my class', 'data-size':'small', 'data-type': type.getType() ) }}
{% endif %}
There is no way to use an if
statement inside the echo
- {{...}}
- that I am aware of, so you need to have 2 echos and use and if-else
instead.
One line code:
{{ check_field( 'class':'my class', 'data-size':'small', 'data-model-pk': AclGroup.id_group, 'checked':(AclGroup.flg_acive == 1 ? true : null) ) }}'
Also this would work, interestingly:
{{ check_field( 'class':'my class', 'data-size':'small', 'data-model-pk': AclGroup.id_group, 'checked':(AclGroup.flg_acive == 1 ? false : null) ) }}'
But I think the first is more logical.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With