public static function initView() { $config = \Phalcon\DI::getDefault()->get('config'); $view = new \Phalcon\Mvc\View(); $view->setTemplateAfter('main'); // for escaping in views $view->setVar('escaper', new \Phalcon\Escaper()); $view->setViewsDir($config->view->dir); return $view; }
/** * Generate a new View object with preset parameters. * * @param array $options * @param \Phalcon\DiInterface $di * @return \Phalcon\Mvc\View */ public static function getView($options = array(), \Phalcon\DiInterface $di = null) { if ($di == null) { $di = \Phalcon\Di::getDefault(); } $defaults = array('base_dir' => DF_INCLUDE_BASE . '/', 'views_dir' => 'modules/frontend/views/scripts/', 'partials_dir' => '', 'layouts_dir' => '../../../../templates/', 'layout' => 'main'); $options = array_merge($defaults, (array) $options); $view = new \Phalcon\Mvc\View(); $view->setDI($di); $eventsManager = new \Phalcon\Events\Manager(); $view->setEventsManager($eventsManager); // Base directory from which all views load. $view->setBasePath($options['base_dir']); $view->setViewsDir($options['views_dir']); // Relative path of main templates. $view->setLayoutsDir($options['layouts_dir']); $view->setTemplateAfter($options['layout']); // Use present directory for partials by default. $view->setPartialsDir($options['partials_dir']); // Register template engines. $view->registerEngines(array(".phtml" => 'Phalcon\\Mvc\\View\\Engine\\Php', ".volt" => function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => function ($templatePath) { $find_replace = array(DF_INCLUDE_BASE => '', '/modules/' => '', '/views/scripts/' => '_', '/' => '_'); $templatePath = str_replace(array_keys($find_replace), array_values($find_replace), $templatePath); return DF_INCLUDE_CACHE . '/volt_' . $templatePath . '.compiled.php'; })); $compiler = $volt->getCompiler(); $compiler->addFunction('helper', function ($resolvedArgs, $exprArgs) use($di) { return '$this->viewHelper->handle(' . $resolvedArgs . ')'; }); return $volt; })); // Register global escaper. $view->setVar('e', new \Phalcon\Escaper()); return $view; }
public function testVoltEngineBuiltInFunctions() { @unlink('unit-tests/views/test11/index.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->setVar('arr', array(1, 2, 3, 4)); $view->setVar('obj', new SomeObject(array(1, 2, 3, 4))); $view->setVar('str', 'hello'); $view->setVar('no_str', 1234); $view->start(); $view->render('test11', 'index'); $view->finish(); $this->assertEquals($view->getContent(), 'Length Array: 4Length Object: 4Length String: 5Length No String: 4Slice Array: 1,2,3,4Slice Array: 2,3Slice Array: 1,2,3Slice Object: 2,3,4Slice Object: 2,3Slice Object: 1,2Slice String: helSlice String: elSlice String: lloSlice No String: 123Slice No String: 23Slice No String: 34'); }
$cachedFilePath = $cacheBaseDir . str_replace(APPLICATION_PATH, '', $templatePath); $cachedFileDir = dirname($cachedFilePath); if (!is_dir($cachedFileDir)) { $umask = umask(00); mkdir($cachedFileDir, 0777, true); umask($umask); } return $cachedFilePath; }, 'compileAlways' => true); $volt = new \Volt($view, $di); $volt->setOptions($options); return $volt; }); $di->set('view', function () use($di) { $view = new \Phalcon\Mvc\View(); $view->registerEngines(array('.volt' => 'volt')); $view->setVar('app_name', $di->getConfig()->application->name); $view->setVar('app_version', $di->getConfig()->application->version); $view->setVar('app_short_name', $di->getConfig()->application->short_name); $view->setVar('app_description', $di->getConfig()->application->description); $view->setVar('user', $di->getSession()->get('user')); $locales = explode(',', $di->getConfig()->application->locales); $locales = array_map('trim', $locales); $view->setVar('locales', $locales); return $view; }); $di->set('pager', function () use($di) { $pager = new \Application\Common\Pager(); $pager->setDi($di); return $pager; });
private function init_view($options = array()) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(APPFULLPATH . $this->_conf->phalcon->viewsDir); $view->registerEngines(array($this->_conf->volt->extn => function ($view, $di) { //$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt = new \Rpp\Extend\Volt($view, $di); $volt->setOptions(array("compiledPath" => "/u/cache/", "compileAlways" => true)); $compiler = $volt->getCompiler(); $compiler->addFunction('getNode', '\\Rpp\\Services\\Get\\Content::node'); $compiler->addFunction('getPart', '\\Rpp\\Services\\Get\\Content::part'); $compiler->addFunction('getGrilla', '\\Rpp\\Services\\Get\\Destacados::portada'); $compiler->addFunction('nUrl', '\\Rpp\\Services\\Get\\Content::nurl'); $compiler->addFunction('gNode', '\\Rpp\\Services\\Get\\Article::node'); $compiler->addFunction('gPart', '\\Rpp\\Services\\Get\\Article::part'); $compiler->addFunction('pUrl', '\\Rpp\\Services\\Get\\Article::nurl'); $compiler->addFunction('TrackUrl', '\\Rpp\\Services\\Get\\UrlTrack::add_params'); $compiler->addFunction('TrackUrlSocial', '\\Rpp\\Services\\Get\\UrlTrack::add_params_social'); $compiler->addFunction('cdn_image', '\\Rpp\\Services\\Get\\UrlMedia::image'); $compiler->addFunction('cdn_replace', '\\Rpp\\Services\\Get\\UrlMedia::replace_cdn'); $compiler->addFunction('df_match', '\\Rpp\\Services\\Get\\Resultados::match'); $compiler->addFunction('df_campeonato', '\\Rpp\\Services\\Get\\Resultados::campeonato'); $compiler->addFunction('df_torneo', '\\Rpp\\Services\\Get\\Dfactory::torneo'); $compiler->addFunction('clima', '\\Rpp\\Services\\Get\\Climas::get'); return $volt; })); $view->setVar("trackurl", true); $view->setVar("host", $this->_conf->server->host); $view->setVar("eversion", $this->_conf->estaticos->version); $view->setVar("ehost", $this->_conf->estaticos->host); $this->_di->set('view', $view); }
$view->setViewsDir(APPFULLPATH . $conf->phalcon->viewsDir); $view->registerEngines(array($conf->volt->extn => function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $compiler = $volt->getCompiler(); $compiler->addFunction('getNode', '\\Rpp\\Services\\Get\\Content::node'); $compiler->addFunction('getPart', '\\Rpp\\Services\\Get\\Content::part'); $compiler->addFunction('nUrl', '\\Rpp\\Services\\Get\\Content::nurl'); $compiler->addFunction('TrackUrl', '\\Rpp\\Services\\Get\\UrlTrack::add_params'); $compiler->addFunction('TrackUrlSocial', '\\Rpp\\Services\\Get\\UrlTrack::add_params_social'); $compiler->addFunction('cdn_image', '\\Rpp\\Services\\Get\\UrlMedia::image'); $compiler->addFunction('df_match', '\\Rpp\\Services\\Get\\Resultados::match'); $compiler->addFunction('df_campeonato', '\\Rpp\\Services\\Get\\Resultados::campeonato'); $compiler->addFunction('df_torneo', '\\Rpp\\Services\\Get\\Dfactory::torneo'); return $volt; })); $view->setVar("trackurl", true); $view->setVar("host", $conf->server->host); $view->setVar("eversion", $conf->estaticos->version); $view->setVar("ehost", $conf->estaticos->host); $di->set('view', $view); $di->set('viewCache', function () use($conf) { $frontCache = new Phalcon\Cache\Frontend\Output(array("lifetime" => $conf->viewcache->lifetime)); /*$cache = new Rpp\Extend\Publish\MemcacheView($frontCache, array( "host" => $conf->viewcache->memcache->host, "port" => $conf->viewcache->memcache->port, "prefix" => $conf->viewcache->memcache->prefix, "statsKey" => $conf->viewcache->memcache->stats, ) );*/ $cache = new Rpp\Extend\Libmemcached($frontCache, array("servers" => array(array('host' => $conf->viewcache->memcache->host, 'port' => $conf->viewcache->memcache->port, 'weight' => 1)), "prefix" => $conf->viewcache->memcache->prefix, "statsKey" => $conf->viewcache->memcache->stats)); $cache->localconnect();
<?php $view = new \Phalcon\Mvc\View(); //A trailing directory separator is required $view->setViewsDir("../app/views/"); // Passing variables to the views, these will be created as local variables $view->setVar("someProducts", $products); $view->setVar("someFeatureEnabled", true); //Start the output buffering $view->start(); //Render all the view hierarchy related to the view products/list.phtml $view->render("products", "list"); //Finish the output buffering $view->finish(); echo $view->getContent();
public function testViewOptions() { $config = array('cache' => array('service' => 'otherCache')); $di = new Phalcon\DI(); $di->set('otherCache', function () { $frontend = new Phalcon\Cache\Frontend\Output(array('lifetime' => 60)); return new Phalcon\Cache\Backend\File($frontend, array('cacheDir' => 'unit-tests/cache/')); }); $view = new Phalcon\Mvc\View($config); $view->setDI($di); $view->setViewsDir('unit-tests/views/'); $date = date("r"); $content = '<html>' . $date . '</html>' . PHP_EOL; $view->setVar("date", $date); $view->start(); $view->cache(true); $view->render('test8', 'other'); $view->finish(); $this->assertEquals($view->getContent(), $content); $view->reset(); sleep(1); $view->setVar("date", date("r")); $view->start(); $view->cache(true); $view->render('test8', 'other'); $view->finish(); $this->assertEquals($view->getContent(), $content); }