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

How can I access persistent value from within volt?

Hi,

I have the following code:

use Phalcon\Mvc\Controller;
class ControllerBase extends Controller
{
    public function initialize()
    { 
        if (!isset($this->persistent->site)){

            $request = new Phalcon\Http\Request;
            $result = Sites::findFirst([
                'columns'    => 'id, name, url, theme',
                'conditions' => 'url = ?1 AND active = ?2', 
                'bind'       => [
                    1 => $request->getServer('HTTP_HOST'),
                    2 => 1,
                ]
            ]);

            if($result){
                $result = $result->toArray();
                $this->persistent->site = $result;
            }else{
                echo 'Redirect to site unavailable';
            }
        }
    }
}

In a controller I can access the theme value by:

$this->persistent->site['theme'];

But how can I access this within volt?

Thanks



6.6k
Accepted
answer

Found the solution:

$this->view->setVars(["site" => $this->persistent->site]);

It's then accessibe within volt by {{ site['theme'] }}