Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phalcon Volt check_field with if else statement for checked

Tags:

php

phalcon

volt

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?

like image 743
user1547410 Avatar asked Sep 16 '15 11:09

user1547410


2 Answers

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.

like image 195
Kvothe Avatar answered Nov 13 '22 15:11

Kvothe


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.

like image 4
Anton Pelykh Avatar answered Nov 13 '22 15:11

Anton Pelykh