Skip to content

Instantly share code, notes, and snippets.

@KorsaR-ZN
Created April 17, 2015 21:33
Show Gist options
  • Save KorsaR-ZN/d8806544e46e7b720366 to your computer and use it in GitHub Desktop.
Save KorsaR-ZN/d8806544e46e7b720366 to your computer and use it in GitHub Desktop.
Phalcon extended view
<?php
/**
* ViewCascade.php 2014-07-23 20:31
* ----------------------------------------------
*
*
* @author Stanislav Kiryukhin <korsar.zn@gmail.com>
* @copyright Copyright (c) 2014, CKGroup.ru
*
* @version 0.0.1
* ----------------------------------------------
* All Rights Reserved.
* ----------------------------------------------
*/
use Phalcon\Mvc\View\Exception;
use Phalcon\Mvc\View;
/**
* Class ViewCascade
*
* <code>
* $view = new ViewCascade();
* $view->addViewsDir(__FOLDER_SOME_1);
* $view->addViewsDir(__FOLDER_SOME_2);
* $view->addViewsDir(__FOLDER_SOME_3);
*
* // Render template found in one of the folders as they are added.
* $view->render()
*
* </code>
*/
class ViewCascade extends View
{
protected $_viewsDirs;
/**
* @var
*/
protected $_eventsManager;
/**
* @param $path
*
* @return $this
*/
public function addViewsDir($path)
{
$this->_viewsDirs[] = $path;
$this->setViewsDir($path);
return $this;
}
/**
* @param $view
* @param array $vars
*
* @return string
*/
public function getPartial($view, $vars = [])
{
ob_start();
$this->partial($view, $vars);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/**
* Checks whether view exists on registered extensions and render it
*
* @param array $engines
* @param string $viewPath
* @param boolean $silence
* @param boolean $mustClean
* @param \Phalcon\Cache\BackendInterface $cache
*
* @throws \Phalcon\Mvc\View\Exception
*/
protected function _engineRender($engines, $viewPath, $silence, $mustClean, $cache = null)
{
if (is_object($cache)) {
//TODO: add implements for cache...
throw new Exception('Cache view not supported...');
return;
}
$viewsDirs = is_array($this->_viewsDirs) ? array_reverse($this->_viewsDirs) : [$this->_viewsDir];
$notExists = true;
$viewEnginePath = null;
foreach ($engines as $extension => $engine) {
foreach ($viewsDirs as $viewsDir) {
$viewsDirPath = $this->_basePath . $viewsDir . $viewPath;
$viewEnginePath = $viewsDirPath . $extension;
if (is_file($viewEnginePath)) {
if (is_object($this->_eventsManager)) {
$this->_activeRenderPath = $viewEnginePath;
if($this->_eventsManager->fire('view:beforeRenderView', $this, $viewEnginePath) === false) {
break;
}
}
$engine->render($viewEnginePath, $this->_viewParams, $mustClean);
if (is_object($this->_eventsManager)) {
$this->_eventsManager->fire('view:afterRenderView', $this);
}
$notExists = false;
break 2;
}
}
}
if ($notExists) {
/**
* Notify about not found views
*/
if (is_object($this->_eventsManager)) {
$this->_activeRenderPath = $viewEnginePath;
$this->_eventsManager->fire('view:notFoundView', $this);
}
if (!$silence) {
$exceptionMessage = 'View "'.($viewPath).'" was not found in the views directories';
throw new Exception($exceptionMessage);
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment