5

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?

doydoy44
  • 5,720
  • 4
  • 29
  • 45
user1547410
  • 863
  • 7
  • 27
  • 58
  • Please provide a html version of what you want to achieve – ka_lin Sep 16 '15 at 11:58
  • 1
    I think that if `AclGroup.flg_active` is Boolean you can just use that like `'checked' : AclGroup.flg_active`. Phalcon will not add the `checked` attribute is it is false. – James Fenwick Sep 16 '15 at 12:41

2 Answers2

5

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.

Kvothe
  • 1,819
  • 2
  • 23
  • 37
4

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.

Anton Pelykh
  • 2,274
  • 1
  • 18
  • 21
  • 1
    Upvoted as this should be the accepted answer really, because it demonstrates the correct use of the checkbox tag checked method. Note that `null` must be used, because an empty string, 0 or `false` will always result in the checkbox being checked. – Ally Jan 05 '16 at 09:36