|
|
@ -17,62 +17,142 @@ use yii\util\FileHelper; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class View extends Component |
|
|
|
class View extends Component |
|
|
|
{ |
|
|
|
{ |
|
|
|
public $file; |
|
|
|
|
|
|
|
public $name; |
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string|array |
|
|
|
* @var Object the owner of this view |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $basePath; |
|
|
|
|
|
|
|
public $owner; |
|
|
|
public $owner; |
|
|
|
public $locale; |
|
|
|
/** |
|
|
|
|
|
|
|
* @var string|array the base path where the view file should be looked for using the specified view name. |
|
|
|
|
|
|
|
* This can be either a string representing a single base path, or an array representing multiple base paths. |
|
|
|
|
|
|
|
* If the latter, the view file will be looked for in the given base paths in the order they are specified. |
|
|
|
|
|
|
|
* This property must be set before calling [[render()]]. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $basePath; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var string the language that the view should be rendered in. If not set, it will use |
|
|
|
|
|
|
|
* the value of [[Application::language]]. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $language; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var string the language that the original view is in. If not set, it will use |
|
|
|
|
|
|
|
* the value of [[Application::sourceLanguage]]. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $sourceLanguage; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Renders a view. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* The method first identifies the actual view file corresponding to the specified view. |
|
|
|
|
|
|
|
* It then calls [[renderFile()]] to render the view file. The rendering result is returned |
|
|
|
|
|
|
|
* as a string. If the view file does not exist, an exception will be thrown. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* To determine which view file should be rendered, the method calls [[findViewFile()]] which |
|
|
|
|
|
|
|
* will search in the directories as specified by [[basePath]]. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* View name can be a path alias representing an absolute file path (e.g. `@app/views/layout/index`), |
|
|
|
|
|
|
|
* or a path relative to [[basePath]]. The file suffix is optional and defaults to `.php` if not given |
|
|
|
|
|
|
|
* in the view name. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param string $view the view to be rendered. This can be a path alias or a path relative to [[basePath]]. |
|
|
|
|
|
|
|
* @param array $params the parameters that should be made available in the view. The PHP function `extract()` |
|
|
|
|
|
|
|
* will be called on this variable to extract the variables from this parameter. |
|
|
|
|
|
|
|
* @throws Exception |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function render($view, $params = array()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$file = $this->findViewFile($view); |
|
|
|
|
|
|
|
if ($file !== false) { |
|
|
|
|
|
|
|
return $this->renderFile($file, $params); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
throw new Exception("Unable to find the view file for view '$view'."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function renderFile($file, $params = array()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->renderFileInternal($file, $params); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function widget($class, $properties = array(), $returnOutput = false) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function beginWidget($class, $properties = array()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function endWidget($returnOutput = false) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function beginClip($id, $properties = array()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function render($_params_ = array()) |
|
|
|
public function endClip() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function beginCache($id, $properties = array()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function endCache() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function beginContent() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function endContent() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function renderFileInternal($_file_, $_params_ = array()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->resolveViewFile(); |
|
|
|
|
|
|
|
extract($_params_, EXTR_OVERWRITE); |
|
|
|
extract($_params_, EXTR_OVERWRITE); |
|
|
|
ob_start(); |
|
|
|
ob_start(); |
|
|
|
ob_implicit_flush(false); |
|
|
|
ob_implicit_flush(false); |
|
|
|
require($this->file); |
|
|
|
require($_file_); |
|
|
|
return ob_get_clean(); |
|
|
|
return ob_get_clean(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function resolveViewFile() |
|
|
|
public function findViewFile($view) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($this->file !== null) { |
|
|
|
if ($view[0] === '/') { |
|
|
|
return $this->file; |
|
|
|
throw new Exception('The view name "$view" should not start with a slash "/".'); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($this->name === null || $this->basePath) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (($extension = FileHelper::getExtension($view)) === '') { |
|
|
|
|
|
|
|
$view .= '.php'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ($view[0] === '@') { |
|
|
|
if(empty($viewName)) |
|
|
|
$file = \Yii::getAlias($view[0]); |
|
|
|
return false; |
|
|
|
} elseif (!empty($this->basePath)) { |
|
|
|
|
|
|
|
$basePaths = is_array($this->basePath) ? $this->basePath : array($this->basePath); |
|
|
|
if($moduleViewPath===null) |
|
|
|
$found = false; |
|
|
|
$moduleViewPath=$basePath; |
|
|
|
foreach ($basePaths as $basePath) { |
|
|
|
|
|
|
|
$file = $basePath . DIRECTORY_SEPARATOR . $view; |
|
|
|
if(($renderer=Yii::app()->getViewRenderer())!==null) |
|
|
|
if (is_file($file)) { |
|
|
|
$extension=$renderer->fileExtension; |
|
|
|
$found = true; |
|
|
|
else |
|
|
|
break; |
|
|
|
$extension='.php'; |
|
|
|
} |
|
|
|
if($viewName[0]==='/') |
|
|
|
} |
|
|
|
{ |
|
|
|
if (!$found) { |
|
|
|
if(strncmp($viewName,'//',2)===0) |
|
|
|
return false; |
|
|
|
$viewFile=$basePath.$viewName; |
|
|
|
} |
|
|
|
else |
|
|
|
|
|
|
|
$viewFile=$moduleViewPath.$viewName; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
else if(strpos($viewName,'.')) |
|
|
|
$file = FileHelper::localize($file, $this->language, $this->sourceLanguage); |
|
|
|
$viewFile=Yii::getPathOfAlias($viewName); |
|
|
|
return is_file($file) ? $file : false; |
|
|
|
else |
|
|
|
|
|
|
|
$viewFile=$viewPath.DIRECTORY_SEPARATOR.$viewName; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(is_file($viewFile.$extension)) |
|
|
|
|
|
|
|
return Yii::app()->findLocalizedFile($viewFile.$extension); |
|
|
|
|
|
|
|
else if($extension!=='.php' && is_file($viewFile.'.php')) |
|
|
|
|
|
|
|
return Yii::app()->findLocalizedFile($viewFile.'.php'); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |