Commit 992f8edc by Qiang Xue

proper implementation of View::viewFile.

parent 97d291ca
...@@ -92,13 +92,12 @@ class View extends Component ...@@ -92,13 +92,12 @@ class View extends Component
* @internal * @internal
*/ */
public $dynamicPlaceholders = []; public $dynamicPlaceholders = [];
/** /**
* @var string the path of the view file currently being rendered. If the view is not * @var array the view files currently being rendered. There may be multiple view files being
* in the process of rendering a view, this property is null. * rendered at a moment because one may render a view file within another.
* This property is mainly provided for information purpose and is maintained by [[renderFile()]].
* Do not modify it.
*/ */
public $viewFile; private $_viewFiles = [];
/** /**
...@@ -223,9 +222,9 @@ class View extends Component ...@@ -223,9 +222,9 @@ class View extends Component
if ($context !== null) { if ($context !== null) {
$this->context = $context; $this->context = $context;
} }
$output = ''; $output = '';
$this->viewFile = $viewFile; $this->_viewFiles[] = $viewFile;
if ($this->beforeRender()) { if ($this->beforeRender()) {
Yii::trace("Rendering view file: $viewFile", __METHOD__); Yii::trace("Rendering view file: $viewFile", __METHOD__);
$ext = pathinfo($viewFile, PATHINFO_EXTENSION); $ext = pathinfo($viewFile, PATHINFO_EXTENSION);
...@@ -241,14 +240,22 @@ class View extends Component ...@@ -241,14 +240,22 @@ class View extends Component
} }
$this->afterRender($output); $this->afterRender($output);
} }
$this->viewFile = null;
array_pop($this->_viewFiles);
$this->context = $oldContext; $this->context = $oldContext;
return $output; return $output;
} }
/** /**
* @return string|boolean the view file currently being rendered. False if no view file is being rendered.
*/
public function getViewFile()
{
return end($this->_viewFiles);
}
/**
* This method is invoked right before [[renderFile()]] renders a view file. * This method is invoked right before [[renderFile()]] renders a view file.
* The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event. * The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event.
* If you override this method, make sure you call the parent implementation first. * If you override this method, make sure you call the parent implementation first.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment