Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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



1.7k
Accepted
answer

Found the solution:

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

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