phalconvolt

Phalcon volt template engine


$di = new \Phalcon\DI\FactoryDefault();

/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function() use ($config) {
    $url = new \Phalcon\Mvc\Url();
    $url->setBaseUri($config->application->baseUri);
    return $url;
});

$di->set('voltService', function()  {
    $volt = new Phalcon\Mvc\View\Engine\Volt($view, $di);
    $volt->setOptions(array(
        "compiledPath" => "../app/compiled/",
        "compiledExtension" => ".php"
    ));
    return $volt;
});

/**
 * Setting up the view component
 */
$di->set('view', function() use ($config) {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array(".phtml" => 'voltService'));
    return $view;
});

I use this article : http://docs.phalconphp.com/en/0.6.0/reference/volt.html ,but tip error message

Notice: Undefined variable: view in /var/www/html/phalconblog/public/index.php on line 40

Notice: Undefined variable: di in /var/www/html/phalconblog/public/index.php on line 40

Fatal error: Call to undefined method Phalcon\Mvc\View\Engine\Volt::setOptions() in /var/www/html/phalconblog/public/index.php on line 42


Solution

  • I tried that too and had similar issues - not sure why.

    EDIT

    The correct way is:

    // Register Volt as a service
    $di->set(
        'volt', 
        function($view, $di) 
        {
            $volt = new Phalcon\Mvc\View\Engine\Volt($view, $di);
    
            $volt->setOptions(
                array(
                    'compiledPath'      => $config->volt->path,
                    'compiledExtension' => $config->volt->extension,
                    'compiledSeparator' => $config->volt->separator,
                    'stat'              => (bool) $config->volt->stat,
                )
            );
    
            return $volt;
        }
    );
    
    // Register Volt as template engine
    $di->set(
        'view', 
        function() 
        {
            $view = new \Phalcon\Mvc\View();
            $view->setViewsDir($config->application->viewsDir);
    
            $view->registerEngines(array(".volt" => 'volt'));
    
            return $view;
        }
    );
    

    The below is a workaround but not advised:

    /**
     * Setting up the view component
     */
    $di->set(
        'view', 
        function() use ($config, $di) 
        {
            $view = new \Phalcon\Mvc\View();
            $view->setViewsDir($config->application->viewsDir);
    
            $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
            $volt->setOptions(
                array(
                    'compiledPath'      => $config->volt->path,
                    'compiledExtension' => $config->volt->extension,
                    'compiledSeparator' => $config->volt->separator,
                    'stat'              => (bool) $config->volt->stat,
                )
            );
    
            /**
             * Register Volt
             */
            $view->registerEngines(array('.phtml' => $volt));
    
            return $view;
        }
    );