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

Add 'hidden' class to button, if function returns false.

I created a protected function isDevelopmentMode() in ControllerBase.php. This class is the base class for all other controllers.

Now I made a button for dummy data. This button should only visible in development mode. So if isDevelopmentMode() returns false the css class is hidden should be applied to the button.

How can I check if function returns true/false and based on that How to add class to button?

edited Nov '17

It is bad idea to show/hide sensitive content with CSS. If not in dev mode the button should not be generated at all.

This function can set a global variable. For example:

function isDevelopmentMode()
{
  $developmentMode = true; // your custom logic here
  $this->view->isDevMode = $developmentMode;
  return $developmentMode; // true or false
}

Above is just a quick exmaple. You can add this Variable in your controller according to isDevelopmentMode() result. This way you will keep the function simple.

And in the template

// Volt
{% if isDevMode === true %}
<button>click</button>
{% endif %}

// Phtml (saw in previous post you asked for Phtml code isntead of Volt)
<?php if ($isDevMode === true): ?>
<button>click</button>
<?php endif; ?>


125.8k
Accepted
answer

I agree that the button should simply not be generated in development mode. Fortunately it's trivial to change.

What I've done with similar problems is added a beforeExecuteRoute method to my base controller. This method acts as an event listener and basically gets run for every request, before the actual action method gets called. It's really useful for setting up stuff that is used on each request. In that method, do what @Nikolay did and set a view variable:

class BaseController{

    public function beforeExecuteRoute(){
        $this->view->isDevMode = $this->isDevelopmentMode();
    }

    public function isDevelopmentMode(){
        ...
    }
}