/** * render the content of an existing view : $controller/$action and set the response to the modal content * @param View $view * @param string $controller a Phalcon controller * @param string $action a Phalcon action * @param $params The parameters to pass to the view */ public function renderContent($view, $controller, $action, $params = NULL) { $template = $view->getRender($controller, $action, $params, function ($view) { $view->setRenderLevel(View::LEVEL_ACTION_VIEW); }); $this->content = $template; }
public function testStandardRender() { $view = new View(); $view->setBasePath(__DIR__ . '/../'); $view->setViewsDir('unit-tests/views/'); $this->assertEquals($view->getViewsDir(), 'unit-tests/views/'); //Standard Render $view->start(); $view->render('test2', 'index'); $view->finish(); $this->assertEquals($view->getContent(), '<html>here</html>' . PHP_EOL); $view->start(); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), '<html>lolhere</html>' . PHP_EOL); //Variables $view->setParamToView('a_cool_var', 'le-this'); $view->start(); $view->render('test3', 'another'); $view->finish(); $this->assertEquals($view->getContent(), '<html>lol<p>le-this</p></html>' . PHP_EOL); //Templates $view->setTemplateAfter('test'); $view->start(); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), '<html>zuplolhere</html>' . PHP_EOL); $view->cleanTemplateAfter(); //Render Levels $view->setRenderLevel(View::LEVEL_MAIN_LAYOUT); $view->start(); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), '<html>lolhere</html>' . PHP_EOL); $view->setRenderLevel(View::LEVEL_LAYOUT); $view->start(); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), 'lolhere'); $view->setRenderLevel(View::LEVEL_ACTION_VIEW); $view->start(); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), 'here'); //Pick View $view->setRenderLevel(View::LEVEL_MAIN_LAYOUT); $view->start(); $view->pick('test3/yup'); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), '<html>lolyup</html>' . PHP_EOL); //No Render $view->setRenderLevel(View::LEVEL_NO_RENDER); $view->start(); $view->pick('test3/yup'); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), ''); }
public function register() { $view = new PhalconView(); $config = $this->getConfig('view'); $configDefault = $this->getConfig('view.default'); // if no engine setted or default == 'null' then // set no render level of view if ('null' == $configDefault) { $view::LEVEL_NO_RENDER; return $view; } $view->setViewsDir($this->getConfig('view.path')); $view->setRenderLevel(PhalconView::LEVEL_LAYOUT); $di = $this->getDI(); $default = 'register' . ucfirst($configDefault); $this->{$default}($view, $di, $config); return $view; }
/** * Tests the View::getActiveRenderPath * * @issue 12139 * @author Serghei Iakovlev <*****@*****.**> * @since 2014-08-14 */ public function testGetActiveRenderPath() { $this->specify('The View::getActiveRenderPath returns unexpected result', function () { $view = new View(); $eventsManager = new Manager(); $eventsManager->attach('view', new ViewAfterRenderListener()); $view->setViewsDir(PATH_DATA . 'views'); $view->setRenderLevel(View::LEVEL_ACTION_VIEW); $view->setEventsManager($eventsManager); expect($view->getActiveRenderPath())->equals(''); $view->start(); $view->render('test15', 'index'); $view->finish(); $view->getContent(); expect($view->getActiveRenderPath())->equals(PATH_DATA . 'views' . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml'); $view->setViewsDir([PATH_DATA . 'views', PATH_DATA . 'views2']); expect($view->getActiveRenderPath())->equals([PATH_DATA . 'views' . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml']); }); }
/** * The Dispatcher component */ $di->setShared('dispatcher', function () use($di) { $eventsManager = $di->getShared('eventsManager'); $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); /** * Setting up the view component */ $di->set('view', function () use($config) { $view = new View(); $view->setViewsDir($config->application->viewsDir); $view->setRenderLevel(View::LEVEL_ACTION_VIEW); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_', 'compileAlways' => true)); //add filter functions $compiler = $volt->getCompiler(); $compiler->addFilter('uniform_time', function ($resolvedArgs, $exprAgs) { return "date('Y-m-d H:i:s', strtotime(" . $resolvedArgs . "))"; }); $compiler->addFilter('number_format', function ($resolveArgs, $exprAgs) { return 'number_format(' . $resolveArgs . ')'; }); $compiler->addFilter('urlencode', function ($resolveArgs, $exprAgs) { return 'urlencode(' . $resolveArgs . ')'; }); return $volt;
/** * registering module-specific services * * @param \DiCustom $di */ public function registerServices(DiInterface $di) { $oLogger = $di->getFileLogger(); // $oRouter = new Router(false); $oRouter = new CustomRouter(false); // $oOldRouter = $di->getRouter(); // Tester::ec('old router: ' . HC::className($oOldRouter)); $di->set('router', $oRouter); // Tester::ec('new router set: ' . HC::className($di->getRouter())); $oRouter->mount(new ApiRoutes($di)); $oVersionLoader = new VersionLoader(); $di->set('versionLoader', $oVersionLoader); // $oApiDispatcherEventsManager = new Manager(); //// $oLogger = $di->getFileLogger(); // $oRouter = $di->getRouter(); // // $oLogger->debug('api module ' . __FUNCTION__ . ': setting up dispatcher'); // // $oApiDispatcherEventsManager->attach('dispatch', function(Event $event, Dispatcher $dispatcher, $data) use($oLogger, $oRouter){ // $oLogger->debug('api dispatcher: ' . $event->getType() . ': ' . print_r($oRouter->getMatchedRoute(), true)); // }); // // // $oDispatcher = $di->getDispatcher(); // $oDispatcher->setDefaultNamespace('App\Modules\Api\Web'); // $oDispatcher->setControllerSuffix('Homorrag'); // $oDispatcher->setEventsManager($oApiDispatcherEventsManager); $oDispatcher = new Dispatcher(); $oApiDispatcherEventsManager = new Manager(); $oApiDispatcherEventsManager->attach('dispatch:beforeDispatchLoop', function (Event $oEvent, Dispatcher $oDispatcher, $data) { /** * @type \DiCustom $di */ $di = Di::getDefault(); $oLogger = $di->getFileLogger(); $arParams = $oDispatcher->getParams(); $oLogger->debug(__CLASS__ . ': ' . $oEvent->getType() . ': trying to dispatch:' . ' module: ' . $oDispatcher->getModuleName() . ' media: ' . $arParams['media'] . ' version: v' . $arParams['major'] . '_' . $arParams['minor'] . ' controller: ' . $oDispatcher->getControllerName() . ' action: ' . $oDispatcher->getActionName()); $di->getVersionLoader()->load(); }); $oApiDispatcherEventsManager->attach('dispatch', function (Event $oEvent, Dispatcher $oDispatcher, $data) { /** * @type \DiCustom $di */ $di = Di::getDefault(); $oLogger = $di->getFileLogger(); // $oRouter = $di->getRouter(); // // $arParams = $oRouter->getParams(); // // $oLogger->debug(__CLASS__ . ': ' . $oEvent->getType() . ': trying to dispatch: from router: ' // . ' module: ' . $oRouter->getModuleName() // . ' media: ' . $arParams['media'] // . ' version: v' . $arParams['major'] . '_' . $arParams['minor'] // . ' controller: ' . $oRouter->getControllerName() // . ' action: ' . $oRouter->getActionName() // ); $arParams = $oDispatcher->getParams(); $oLogger->debug(__CLASS__ . ': ' . $oEvent->getType() . ': trying to dispatch: from dispatcher: ' . ' module: ' . $oDispatcher->getModuleName() . ' media: ' . $arParams['media'] . ' version: v' . $arParams['major'] . '_' . $arParams['minor'] . ' controller: ' . $oDispatcher->getControllerName() . ' action: ' . $oDispatcher->getActionName()); // $oLogger->debug(__CLASS__ . ': ' . $oEvent->getType()); }); $oDispatcher->setEventsManager($oApiDispatcherEventsManager); $di->setShared('dispatcher', $oDispatcher); // $di->set('dispatcher', function() use($di){ // $dispatcher = new Dispatcher(); // $oApiDispatcherEventsManager = new Manager(); // $oLogger = $di->getFileLogger(); // $oRouter = $di->getRouter(); // $oRequest = $di->getRequest(); // // $oLogger->debug('api module ' . __FUNCTION__ . ': setting up dispatcher'); // // $oApiDispatcherEventsManager->attach('dispatch', function(Event $event, Dispatcher $dispatcher, $data) use($oLogger, $oRouter, $oRequest){ // // if($event->getType() == 'beforeDispatchLoop'){ // // $arRoutes = $oRouter->getRoutes(); // // foreach ($arRoutes as $oRoute) { // $oRoute->beforeMatch(function($uri, $route) use ($oLogger){ // $oLogger->debug('__ api module dispatcher route beforeMatch: ' . $uri . $route); // // }); // $oLogger->debug('api module dispatcher: ' . $event->getType() . ': route registered: ' . $oRoute->getCompiledPattern()); // // $regPattern = $oRoute->getCompiledPattern(); // // $strUri = $oRequest->getURI(); // // if(preg_match($regPattern, $strUri)){ // $oLogger->debug('"' . $strUri . '" matched ' . $regPattern); // }else{ // $oLogger->debug('"' . $strUri . '" mismatched ' . $regPattern); // } // // } // // } // // $oLogger->debug('api dispatcher: ' . $event->getType() . ': route matched: ' . print_r($oRouter->getMatchedRoute(), true)); // $oLogger->debug('api dispatcher: ' . $event->getType() // . ' module "' . $oRouter->getModuleName() // . '" controller: "' . $oRouter->getControllerName() // . '" action: "' . $oRouter->getActionName() . '"' // ); // }); // // $dispatcher->setEventsManager($oApiDispatcherEventsManager); // $dispatcher->setDefaultNamespace('App\Modules\Api\Web'); // return $dispatcher; // }); // // // // $oConfig = new Config(array( // 'application' => array( // 'viewsDir' => __DIR__ . '/views', // 'cacheDir' => __DIR__ . '/../../../var/cache/regular' // ), // )); // // $di->get('config')->merge($oConfig); /** * Setting up the view component */ $di->set('view', function () use($oLogger) { $oView = new View(); $oView->setRenderLevel(View::LEVEL_NO_RENDER); $oView->disable(); $oLogger->debug('view: render level set to disabled'); //Disable several levels // $view->disableLevel(array( // View::LEVEL_LAYOUT => true, // View::LEVEL_MAIN_LAYOUT => true // )); return $oView; }, true); }
public function testIssue907() { $view = new View(); $view->setBasePath(__DIR__ . '/../'); $view->setViewsDir('unit-tests/views/'); $listener = new \ViewAfterRenderListener(); $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach('view', $listener); $view->setEventsManager($eventsManager); $view->start(); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), '<html>lolhere</html>' . PHP_EOL); $this->assertEquals('1,3,6', $listener->getLevels()); $listener->reset(); //Templates $view->setTemplateAfter('test'); $view->start(); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), '<html>zuplolhere</html>' . PHP_EOL); $this->assertEquals('1,3,5,6', $listener->getLevels()); $listener->reset(); $view->cleanTemplateAfter(); //Render Levels $view->setRenderLevel(View::LEVEL_MAIN_LAYOUT); $view->start(); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), '<html>lolhere</html>' . PHP_EOL); $this->assertEquals('1,3,6', $listener->getLevels()); $listener->reset(); $view->setRenderLevel(View::LEVEL_LAYOUT); $view->start(); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), 'lolhere'); $this->assertEquals('1,3', $listener->getLevels()); $listener->reset(); $view->setRenderLevel(View::LEVEL_ACTION_VIEW); $view->start(); $view->render('test3', 'other'); $view->finish(); $this->assertEquals($view->getContent(), 'here'); $this->assertEquals('1', $listener->getLevels()); $listener->reset(); }
public function testVoltEngine() { $this->specify("Volt engine doesn't work as expected", function () { @unlink(PATH_DATA . 'views/layouts/test10.volt.php'); @unlink(PATH_DATA . 'views/test10/index.volt.php'); @unlink(PATH_DATA . 'views/test10/other.volt.php'); $di = new Di(); $view = new View(); $view->setDI($di); $view->setViewsDir(PATH_DATA . 'views/'); $view->registerEngines(array('.volt' => 'Phalcon\\Mvc\\View\\Engine\\Volt')); $view->setParamToView('song', 'Rock n roll'); $view->start(); $view->setRenderLevel(View::LEVEL_ACTION_VIEW); $view->render('test10', 'index'); $view->finish(); expect($view->getContent())->equals('Hello Rock n roll!'); $view->setParamToView('some_eval', true); $view->start(); $view->setRenderLevel(View::LEVEL_LAYOUT); $view->render('test10', 'index'); $view->finish(); expect($view->getContent())->equals('Clearly, the song is: Hello Rock n roll!.' . PHP_EOL); //Refreshing generated view file_put_contents(PATH_DATA . 'views/test10/other.volt', '{{song}} {{song}}'); $view->setParamToView('song', 'Le Song'); $view->start(); $view->setRenderLevel(View::LEVEL_ACTION_VIEW); $view->render('test10', 'other'); $view->finish(); expect($view->getContent())->equals('Le Song Le Song'); $view->start(); $view->setRenderLevel(View::LEVEL_LAYOUT); $view->render('test10', 'other'); $view->finish(); expect($view->getContent())->equals('Clearly, the song is: Le Song Le Song.' . PHP_EOL); //Change the view file_put_contents(PATH_DATA . 'views/test10/other.volt', 'Two songs: {{song}} {{song}}'); $view->start(); $view->setRenderLevel(View::LEVEL_LAYOUT); $view->render('test10', 'other'); $view->finish(); expect($view->getContent())->equals('Clearly, the song is: Two songs: Le Song Le Song.' . PHP_EOL); }); }
/** * Tests View::render * * @author Andres Gutierrez <*****@*****.**> * @since 2012-03-05 */ public function testStandardRender() { $this->specify('The View component does not work as expected', function () { $view = new View(); $view->setViewsDir(PATH_DATA . 'views' . DIRECTORY_SEPARATOR); $view->start(); $view->render('test2', 'index'); $view->finish(); expect($view->getContent())->equals("<html>We are here</html>\n"); $view->start(); $view->render('test3', 'other'); $view->finish(); expect($view->getContent())->equals("<html>lolhere</html>\n"); $view->setParamToView('a_cool_var', 'le-this'); $view->start(); $view->render('test3', 'another'); $view->finish(); expect($view->getContent())->equals("<html>lol<p>le-this</p></html>\n"); $view->setTemplateAfter('test'); $view->start(); $view->render('test3', 'other'); $view->finish(); expect($view->getContent())->equals("<html>zuplolhere</html>\n"); $view->cleanTemplateAfter(); $view->setRenderLevel(View::LEVEL_MAIN_LAYOUT); $view->start(); $view->render('test3', 'other'); $view->finish(); expect($view->getContent())->equals("<html>lolhere</html>\n"); $view->setRenderLevel(View::LEVEL_LAYOUT); $view->start(); $view->render('test3', 'other'); $view->finish(); expect($view->getContent())->equals('lolhere'); $view->setRenderLevel(View::LEVEL_ACTION_VIEW); $view->start(); $view->render('test3', 'other'); $view->finish(); expect($view->getContent())->equals('here'); $view->setRenderLevel(View::LEVEL_MAIN_LAYOUT); $view->start(); $view->pick('test3/yup'); $view->render('test3', 'other'); $view->finish(); expect($view->getContent())->equals("<html>lolyup</html>\n"); $view->setRenderLevel(View::LEVEL_NO_RENDER); $view->start(); $view->pick('test3/yup'); $view->render('test3', 'other'); $view->finish(); expect($view->getContent())->equals(''); }); }
public function testVoltEngine() { @unlink('unit-tests/views/layouts/test10.volt.php'); @unlink('unit-tests/views/test10/index.volt.php'); @unlink('unit-tests/views/test10/other.volt.php'); $di = new Phalcon\DI(); $view = new Phalcon\Mvc\View(); $view->setDI($di); $view->setViewsDir('unit-tests/views/'); $view->registerEngines(array('.volt' => 'Phalcon\\Mvc\\View\\Engine\\Volt')); $view->setParamToView('song', 'Rock n roll'); $view->start(); $view->setRenderLevel(Phalcon\Mvc\View::LEVEL_ACTION_VIEW); $view->render('test10', 'index'); $view->finish(); $this->assertEquals($view->getContent(), 'Hello Rock n roll!'); $view->setParamToView('some_eval', true); $view->start(); $view->setRenderLevel(Phalcon\Mvc\View::LEVEL_LAYOUT); $view->render('test10', 'index'); $view->finish(); $this->assertEquals($view->getContent(), 'Clearly, the song is: Hello Rock n roll!.' . PHP_EOL); //Refreshing generated view file_put_contents('unit-tests/views/test10/other.volt', '{{song}} {{song}}'); $view->setParamToView('song', 'Le Song'); $view->start(); $view->setRenderLevel(Phalcon\Mvc\View::LEVEL_ACTION_VIEW); $view->render('test10', 'other'); $view->finish(); $this->assertEquals($view->getContent(), 'Le Song Le Song'); $view->start(); $view->setRenderLevel(Phalcon\Mvc\View::LEVEL_LAYOUT); $view->render('test10', 'other'); $view->finish(); $this->assertEquals($view->getContent(), 'Clearly, the song is: Le Song Le Song.' . PHP_EOL); //Change the view file_put_contents('unit-tests/views/test10/other.volt', 'Two songs: {{song}} {{song}}'); $view->start(); $view->setRenderLevel(Phalcon\Mvc\View::LEVEL_LAYOUT); $view->render('test10', 'other'); $view->finish(); $this->assertEquals($view->getContent(), 'Clearly, the song is: Two songs: Le Song Le Song.' . PHP_EOL); }
protected function _renderJson(View $view) { $this->getResponse()->setContentType('application/json')->setJsonContent($view->getParamsToView())->send(); $view->setRenderLevel(View::LEVEL_NO_RENDER); }