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

Problem with boolean (Controller to view)

Hello guys, I'm trying to pass a boolean to view, but not setting (i guess). Look the code im using:

$this->view->mheader = false;

But when i try to use like the code above it fails:

{% if mheader == false %} ok, mheader is false {% endif %}

If i try with string 'false', it's ok! Someone can help to understand it? Thanks

Forgot: php 7, Phalcon 3.0.1



85.5k
Accepted
answer
edited Nov '16

in controller


$this->view->setVar("mheader", true);

in view


{% set mheader = this.view.getVar("mheader") %}

{{ dump (mheader) }}

@Izo Why the simple way doesnt work? Can u explain for me?

in controller


$this->view->setVar("mheader", true);

in view


{% set mheader = this.view.getVar("mheader") %}

{{ dump (mheader) }}


85.5k

your way isnt exacly "simple" its just wrong :D

You need to call a method in order to set what the view has to expect as params.

https://github.com/phalcon/cphalcon/blob/23b4100faa3ef91863d7f05de3f1c7a450cd52bc/phalcon/mvc/view.zep#L446

I'm just usign that have in the docs, look: https://docs.phalcon.io/pt/latest/reference/views.html#transfer-values-from-the-controller-to-views

your way isnt exacly "simple" its just wrong :D

You need to call a method in order to set what the view has to expect as params.

https://github.com/phalcon/cphalcon/blob/23b4100faa3ef91863d7f05de3f1c7a450cd52bc/phalcon/mvc/view.zep#L446



85.5k

you should avoind using stuff that contains the world "magic" :D

Well, i dont know to be honest, i think there are some issues with php7 and views, maybe its because of that. I always use set/get var in order to be save.

You could cook a small test case and report an issue in github

edited Nov '16

Simply put, this statement:

$this->view->mheader = false;

Just nullifies your view variable with a name 'mheader'. That's it. Actually, it sets property mheader belonging to a view object to false.

What you actually want here is to pass some value to the view component, not to false it / null it. You could do it with string. You could do it with a number (0) too.

I believe the general overview would be that the view component/service expects some value (i.e. string). Boolean is more low level construct, which needs pure PHP to make use of it. Volt would ditch it.

Now why this works:

$this->view->setVar("mheader", true);

For a simple fact that this setVar method will keep value of the var no matter what the type of this var is.

Yea Jonathan, thank you very much! mheader was being nullified

Now i really understand =) Thank you

Simply put, this statement:

$this->view->mheader = false;

Just nullifies your view variable with a name 'mheader'. That's it. Actually, it sets property mheader belonging to a view object to false.

What you actually want here is to pass some value to the view component, not to false it / null it. You could do it with string. You could do it with a number (0) too.

I believe the general overview would be that the view component/service expects some value (i.e. string). Boolean is more low level construct, which needs pure PHP to make use of it. Volt would ditch it.

Now why this works:

$this->view->setVar("mheader", true);

For a simple fact that this setVar method will keep value of the var no matter what the type of this var is.