Skip to content

Commit

Permalink
Adding constants and a property to track the current type.
Browse files Browse the repository at this point in the history
This helps with resolving parent elements.
  • Loading branch information
markstory committed Dec 18, 2011
1 parent 9b2fd8f commit d8c6594
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions lib/Cake/View/View.php
Expand Up @@ -248,7 +248,7 @@ class View extends Object {
protected $_parents = array();

/**
* The currently rendering view file.
* The currently rendering view file. Used for resolving parent files.
*
* @var string
*/
Expand All @@ -258,9 +258,9 @@ class View extends Object {
* Currently rendering an element. Used for finding parent fragments
* for elements.
*
* @var boolean
* @var string
*/
protected $_inElement = false;
protected $_currentType = '';

/**
* Content stack, used for nested templates that all use View::extend();
Expand All @@ -269,6 +269,10 @@ class View extends Object {
*/
protected $_stack = array();

const TYPE_VIEW = 'view';
const TYPE_ELEMENT = 'element';
const TYPE_LAYOUT = 'layout';

/**
* Constructor
*
Expand Down Expand Up @@ -353,9 +357,8 @@ public function element($name, $data = array(), $options = array()) {
$this->Helpers->trigger('beforeRender', array($file));
}

$this->_inElement = true;
$this->_currentType = self::TYPE_ELEMENT;
$element = $this->_render($file, array_merge($this->viewVars, $data));
$this->_inElement = false;

if ($callbacks) {
$this->Helpers->trigger('afterRender', array($file, $element));
Expand Down Expand Up @@ -400,6 +403,7 @@ public function render($view = null, $layout = null) {
$this->output = null;

if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
$this->_currentType = self::TYPE_VIEW;
$this->Helpers->trigger('beforeRender', array($viewFileName));
$this->output = $this->_render($viewFileName);
$this->Helpers->trigger('afterRender', array($viewFileName));
Expand Down Expand Up @@ -462,7 +466,7 @@ public function renderLayout($content_for_layout, $layout = null) {
if (!isset($this->viewVars['title_for_layout'])) {
$this->viewVars['title_for_layout'] = Inflector::humanize($this->viewPath);
}

$this->_currentType = self::TYPE_LAYOUT;
$this->output = $this->_render($layoutFileName);

if ($this->output === false) {
Expand Down Expand Up @@ -618,10 +622,17 @@ public function end() {
* @return void
*/
public function extend($name) {
if ($this->_inElement) {
$parent = $this->_getElementFileName($name);
} else {
$parent = $this->_getViewFileName($name);
switch ($this->_currentType) {
case self::TYPE_VIEW:
$parent = $this->_getViewFileName($name);
break;
case self::TYPE_ELEMENT:
$parent = $this->_getElementFileName($name);
break;
case self::TYPE_LAYOUT:
$parent = $this->_getLayoutFileName($name);
break;

}
$this->_parents[$this->_current] = $parent;
}
Expand Down

0 comments on commit d8c6594

Please sign in to comment.