We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

phalcon volt bitwise operatios?

I need to run the following logic in the volt templates however it seems to be it doesnt support it. ANy ideas on workarounds ?

{% for index, p_key in partner_var %} <input id="{{ key }}[]" name="{{ key }}[]" value="{{ p_key.id }}" type="checkbox"
{% if user.p_body & (1 << (p_key.id - 1)) %} checked {% endif %}> {{ p_key.title }}

fails with the Error Scanning error before ' (1 << (p_key.id.

Hey, i could not find anything about bitwise ops in docs as well :(

However you are free to use php in volt as well. Its not a clean solution, but... :

<?php .... ?>

Another option woud be to extend volt with a function https://docs.phalcon.io/en/latest/reference/volt.html#extending-volt

Extend volt with function which will calculate your bitwise operation nad return what you want

I think it could also be argued that if you're doing bitwise operations in your view, you shouldn't be doing bitwise operations in your view. Do that in the controller.



13.4k

i dont agree, all this doesnt is mark the checkboxes as selected based on the user selected values, i dont think you should add this presentation logic to controllers, it should be handled by the view itself.

i like the solution of adding the function to the volt compiler...

I think it could also be argued that if you're doing bitwise operations in your view, you shouldn't be doing bitwise operations in your view. Do that in the controller.