/** * @param string $moduleName * @param string $modulePath */ public function register($moduleName, $modulePath) { $pPath = $this->dependencyInjection->get('config')->projectPath; $pDir = substr($pPath, 0, strlen($pPath) - 10); $relModulePath = substr($modulePath, strlen($pDir)); $x = implode('/', array_map(function () { return '..'; }, explode('/', str_replace('//', '/', $relModulePath)))); $this->view->setMainView('/../' . $x . '/common/views/index'); $this->view->setPartialsDir('/../' . $x . '/common/views/partial/'); $this->view->setViewsDir($modulePath . '/views/'); $this->registerEngine(); $this->dependencyInjection->set('view', $this->view); }
/** * * @param type $options */ protected function initView($options = []) { $config = $this->_di->get('config'); $di = $this->_di; if (!file_exists($config->volt->path)) { mkdir($config->volt->path, 0777, true); } $this->_di->setShared('volt', function ($view, $di) use($config) { $volt = new Volt($view, $di); $volt->setOptions(['compiledPath' => $config->volt->path, 'compiledExtension' => $config->volt->extension, 'compiledSeparator' => $config->volt->separator, 'compileAlways' => (bool) $config->volt->compileAlways, 'stat' => (bool) $config->volt->stat]); $compiler = $volt->getCompiler(); $compiler->addFunction('is_a', 'is_a'); return $volt; }); $this->_di->setShared('view', function () use($config) { $view = new View(); $view->setViewsDir($config->application->viewsDir); $view->setMainView('index'); $view->setLayoutsDir('layouts/'); $view->setPartialsDir('partials/'); $view->registerEngines(['.volt' => 'volt', '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php']); return $view; }); }
public function testPartials() { $view = new View(); $view->setBasePath(__DIR__ . '/../'); $view->setViewsDir('unit-tests/views/'); $view->setParamToView('cool_var', 'le-this'); // Single partial $view->start(); $view->render('test5', 'index'); $view->finish(); $this->assertEquals($view->getContent(), '<html>Hey, this is a partial, also le-this</html>' . PHP_EOL); // Multiple partials $view->start(); $view->render('test9', 'index'); $view->finish(); $this->assertEquals($view->getContent(), '<html>Hey, this is a partial, also le-this<br />Hey, this is a second partial, also le-this</html>' . PHP_EOL); // A partial within other partial $view->start(); $view->render('test5', 'subpartial'); $view->finish(); $this->assertEquals('<html>Including Hey, this is a partial, also le-this</html>' . PHP_EOL, $view->getContent()); // Single partial in overridden main view $view->setMainView('html5'); $view->start(); $view->render('test5', 'index'); $view->finish(); $this->assertEquals('<!DOCTYPE html><html>Hey, this is a partial, also le-this</html>' . PHP_EOL, $view->getContent()); }
public static function run() { if (in_array(APPLICATION_ENV, array('development'))) { $debug = new \Phalcon\Debug(); $debug->listen(); } $di = new \Phalcon\DI\FactoryDefault(); $config = (include APPLICATION_PATH . '/config/application.php'); $di->set('config', $config); $loader = new \Phalcon\Loader(); $loader->registerNamespaces($config->loader->namespaces->toArray()); $loader->register(); $loader->registerDirs(array(APPLICATION_PATH . "/plugins/")); $db = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "charset" => $config->database->charset)); $di->set('db', $db); $view = new View(); /* $view->disableLevel(array( View::LEVEL_BEFORE_TEMPLATE => true, View::LEVEL_LAYOUT => true, View::LEVEL_AFTER_TEMPLATE => true )); */ define('MAIN_VIEW_PATH', '../../../views/'); $view->setMainView(MAIN_VIEW_PATH . 'main'); $view->setLayoutsDir(MAIN_VIEW_PATH . '/layouts/'); $view->setPartialsDir(MAIN_VIEW_PATH . '/partials/'); $volt = new \Application\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => APPLICATION_PATH . '/cache/volt/')); $volt->initCompiler(); $viewEngines = array(".volt" => $volt); $view->registerEngines($viewEngines); $di->set('view', $view); $viewSimple = new \Phalcon\Mvc\View\Simple(); $viewSimple->registerEngines($viewEngines); $di->set('viewSimple', $viewSimple); $url = new \Phalcon\Mvc\Url(); $url->setBasePath('/'); $url->setBaseUri('/'); $application = new \Phalcon\Mvc\Application(); $application->registerModules($config->modules->toArray()); $router = new \Application\Mvc\Router\DefaultRouter(); foreach ($application->getModules() as $module) { $className = str_replace('Module', 'Routes', $module['className']); if (class_exists($className)) { $class = new $className(); $router = $class->init($router); } } $di->set('router', $router); $eventsManager = new \Phalcon\Events\Manager(); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) { new ExceptionPlugin($dispatcher, $exception); }); $eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) { new LocalizationPlugin($dispatcher); }); $eventsManager->attach("acl", function ($event, $acl) { if ($event->getType() == 'beforeCheckAccess') { echo $acl->getActiveRole(), $acl->getActiveResource(), $acl->getActiveAccess(); } }); $dispatcher->setEventsManager($eventsManager); $di->set('dispatcher', $dispatcher); $session = new \Phalcon\Session\Adapter\Files(); $session->start(); $di->set('session', $session); $acl = new \Application\Acl\DefaultAcl(); $acl->setEventsManager($eventsManager); $di->set('acl', $acl); $assets = new \Phalcon\Assets\Manager(); $di->set('assets', $assets); $flash = new \Phalcon\Flash\Session(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning')); $di->set('flash', $flash); $di->set('helper', new \Application\Mvc\Helper()); $application->setDI($di); echo $application->handle()->getContent(); }
/** * Tests rendering with partials * * @author Andres Gutierrez <*****@*****.**> * @since 2012-05-28 */ public function testRenderingWithPartials() { $this->specify('The rendering with partials does not work as expected', function () { $view = new View(); $view->setViewsDir(PATH_DATA . 'views' . DIRECTORY_SEPARATOR); $view->setParamToView('cool_var', 'le-this'); $view->start(); $view->render('test5', 'index'); $view->finish(); expect($view->getContent())->equals("<html>Hey, this is a partial, also le-this</html>\n"); $view->start(); $view->render('test9', 'index'); $view->finish(); expect($view->getContent())->equals("<html>Hey, this is a partial, also le-this<br />Hey, this is a second partial, also le-this</html>\n"); $view->start(); $view->render('test5', 'subpartial'); $view->finish(); expect($view->getContent())->equals("<html>Including Hey, this is a partial, also le-this</html>\n"); $view->setMainView('html5'); $view->start(); $view->render('test5', 'index'); $view->finish(); expect($view->getContent())->equals("<!DOCTYPE html><html>Hey, this is a partial, also le-this</html>\n"); expect($view->getPartial('partials/_partial1', ['cool_var' => 'le-this']))->equals('Hey, this is a partial, also le-this'); }); }