/** * @param Event $event * @param Application $application * @return mixed */ public function boot(Event $event, Application $application) { $config = $application->getConfig()->application; if (isset($config->view) && $config->view !== false) { $view = new View(); $view->setViewsDir($config->view->viewsDir); $view->setLayoutsDir($config->view->layoutsDir); $view->setLayout($config->view->layout); $application->getDI()->setShared('view', $view); } }
/** * Register specific services for the module */ function registerServices(\Phalcon\DiInterface $di) { //Registering a dispatcher $di->set('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('Modules\\Frontend\\Controllers'); return $dispatcher; }); $config = $di->getShared('config'); $di->set('view', function () use($config, $di) { $view = new View(); $router = $di->getShared('router'); /* * @todo 给layouts等目录统一变量 * */ $view->setViewsDir(__DIR__ . '/views/'); $view->setLayoutsDir('/../../../layouts/'); // 多模块的话, 可能会有多个风格,所以需要切换layout,这里的Path是根据当前module的Path向上走的 $view->setLayout('index'); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_')); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }, true); }
private function setView() { $view = new View(); $view->setViewsDir($this->config->application->viewsDir); $view->setLayoutsDir('layouts/'); $view->setLayout('index'); $view->registerEngines(['.volt' => $this->setVolt($view), '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php']); return $view; }
/** * @param string $layout * * @return string */ public function render($layout = 'edit') { $column = $this->_params->getParam('name'); $prefix = $this->_params->getParam('source'); $fieldId = ($prefix ? $prefix . '_' : '') . $column; $fieldName = $prefix ? $prefix . '[' . $column . ']' : $column; $defaultParams = $this->_params->getParams(); unset($defaultParams['name']); $tagParams = array_merge([$fieldName], $this->_params->getParam('tag', []), ['value' => $this->getValue(), 'id' => $fieldId] + $defaultParams); $this->_params->setParam('id', $fieldId); // все параметры уже добавлены, в общем списке они не нужны unset($tagParams['tag']); $tagParams['class'] = isset($tagParams['class']) ? $tagParams['class'] . ' form-control' : 'form-control'; $layout = $this->_params->getParam('options.layout', $layout); $this->beforeRender($tagParams); $this->_view->setLayout($layout); $this->_view->setVars(['field' => $this, 'model' => $this->_model, 'params' => $this->_params, 'tag' => $tagParams]); return $this->_view->getRender($this->getLayoutDir($layout), $layout); }
}); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($config) { $url = new UrlResolver(); $url->setBaseUri($config->application->baseUri); return $url; }, true); /** * Setting up the view component */ $di->set('view', function () use($config) { $view = new View(); $view->setViewsDir($config->application->viewsDir); $view->setLayout('default'); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_')); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }, true); /** * Database connection is created based in the parameters defined in the configuration file */ $di->set('db', function () use($config) { return new DbAdapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname)); }); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise
public function testOverrideLayout() { $view = new Phalcon\Mvc\View(); $view->setViewsDir('unit-tests/views/'); //Override controller layout $view->start(); $view->setLayout('test6'); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), '<html>Well, this is the view content: here.</html>' . PHP_EOL); }
/** * If the configuration specify the use of metadata adapter use it or use memory otherwise */ $di->set('modelsMetadata', function () { return new MetaDataAdapter(); }); /** * Start the session the first time some component request the session service */ $di->set('session', function () { $session = new SessionAdapter(); $session->start(); return $session; }); $di->set('view', function () use($config, $di) { $view = new View(); $router = $di->getShared('router'); /* * @todo 给layouts等目录统一变量 * */ $view->setViewsDir(__DIR__ . '/views/'); $view->setLayoutsDir('/../../../layouts/'); // 多模块的话, 可能会有多个风格,所以需要切换layout,这里的Path是根据当前module的Path向上走的 $view->setLayout('adminCommon'); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_')); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }, true);
public function testLayoutAndPick() { $view = new View(); $view->setViewsDir('unit-tests/views/'); // Pick string $view->start(); $view->setLayout('test6'); $view->pick('test3/other'); $view->render('test3', 'another'); $view->finish(); $this->assertEquals($view->getContent(), '<html>Well, this is the view content: here.</html>' . PHP_EOL); // Pick array $view->start(); $view->setLayout('test6'); $view->pick(array('test3/other')); $view->render('test3', 'another'); $view->finish(); $this->assertEquals($view->getContent(), '<html>Well, this is the view content: here.</html>' . PHP_EOL); }
/** * Test using different layout and pick * * @author Andres Gutierrez <*****@*****.**> * @since 2013-01-07 */ public function testLayoutAndPick() { $this->specify('Using different layout and pick does not work as expected', function () { $view = new View(); $view->setDI(Di::getDefault()); $view->setViewsDir(PATH_DATA . 'views' . DIRECTORY_SEPARATOR); $view->start(); $view->setLayout('test6'); $view->pick('test3/other'); $view->render('test3', 'another'); $view->finish(); expect($view->getContent())->equals("<html>Well, this is the view content: here.</html>\n"); $view->start(); $view->setLayout('test6'); $view->pick(['test3/other']); $view->render('test3', 'another'); $view->finish(); expect($view->getContent())->equals("<html>Well, this is the view content: here.</html>\n"); }); }