Phalcon伏特模板引擎

时间:2012-10-31 08:13:28

标签: phalcon volt

$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;
});

我使用这篇文章:http://docs.phalconphp.com/en/0.6.0/reference/volt.html,但提示错误消息

  

注意:未定义的变量:查看   第40行的/var/www/html/phalconblog/public/index.php

     

注意:未定义的变量:di in   第40行的/var/www/html/phalconblog/public/index.php

     

致命错误:调用未定义的方法   Phalcon \ Mvc \ View \ Engine \ Volt :: setOptions()in   第42行的/var/www/html/phalconblog/public/index.php

1 个答案:

答案 0 :(得分:3)

我也尝试过这样的问题 - 不确定原因。

修改

正确的方法是:

// 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;
    }
);

以下是解决方法但不建议:

/**
 * 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;
    }
);