類別 yii\web\View
View 代表 MVC 模式中的視圖物件。
View 提供了一系列方法 (例如 render()) 用於渲染目的。
View 預設在 yii\base\Application 中被配置為應用程式元件。您可以透過 Yii::$app->view
存取該實例。
您可以透過在您的應用程式設定檔中的 components 下新增一個陣列來修改其配置,如下列範例所示
'view' => [
'theme' => 'app\themes\MyTheme',
'renderers' => [
// you may add Smarty or Twig renderer here
]
// ...
]
關於 View 的更多詳細資訊和使用方法,請參閱 視圖指南文章。
公共屬性
公共方法
受保護的方法
方法 | 描述 | 定義於 |
---|---|---|
findViewFile() | 根據給定的視圖名稱尋找視圖檔案。 | yii\base\View |
getRequestedViewFile() | yii\base\View | |
registerAssetFiles() | 註冊資源包提供的所有檔案,包括相依資源包檔案。 | yii\web\View |
renderBodyBeginHtml() | 渲染要插入到 body 區段開頭的內容。 | yii\web\View |
renderBodyEndHtml() | 渲染要插入到 body 區段結尾的內容。 | yii\web\View |
renderHeadHtml() | 渲染要插入到 head 區段的內容。 | yii\web\View |
事件
事件 | 類型 | 描述 | 定義於 |
---|---|---|---|
EVENT_AFTER_RENDER | yii\base\ViewEvent | 由 renderFile() 在渲染視圖檔案後立即觸發的事件。 | yii\base\View |
EVENT_BEFORE_RENDER | yii\base\ViewEvent | 由 renderFile() 在渲染視圖檔案之前立即觸發的事件。 | yii\base\View |
EVENT_BEGIN_BODY | yii\web\Event | 由 beginBody() 觸發的事件。 | yii\web\View |
EVENT_BEGIN_PAGE | yii\base\Event | 由 beginPage() 觸發的事件。 | yii\base\View |
EVENT_END_BODY | yii\web\Event | 由 endBody() 觸發的事件。 | yii\web\View |
EVENT_END_PAGE | yii\base\Event | 由 endPage() 觸發的事件。 | yii\base\View |
常數
常數 | 值 | 描述 | 定義於 |
---|---|---|---|
PH_BODY_BEGIN | '' | 這在內部用作接收為 body 區段開頭註冊的內容的預留位置。 | yii\web\View |
PH_BODY_END | ' ' | 這在內部用作接收為 body 區段結尾註冊的內容的預留位置。 | yii\web\View |
PH_HEAD | ' ' | 這在內部用作接收為 head 區段註冊的內容的預留位置。 | yii\web\View |
POS_BEGIN | 2 | 已註冊的 JavaScript 程式碼區塊或檔案的位置。這表示位置在 body 區段的開頭。 | yii\web\View |
POS_END | 3 | 已註冊的 JavaScript 程式碼區塊或檔案的位置。這表示位置在 body 區段的結尾。 | yii\web\View |
POS_HEAD | 1 | 已註冊的 JavaScript 程式碼區塊或檔案的位置。這表示位置在 head 區段中。 | yii\web\View |
POS_LOAD | 5 | 已註冊的 JavaScript 程式碼區塊的位置。這表示 JavaScript 程式碼區塊將包含在 jQuery(window).load() 中。 |
yii\web\View |
POS_READY | 4 | 已註冊的 JavaScript 程式碼區塊的位置。這表示 JavaScript 程式碼區塊將包含在 jQuery(document).ready() 中。 |
yii\web\View |
屬性詳細資訊
已註冊的資源包列表。鍵是資源包名稱,值是已註冊的 yii\web\AssetBundle 物件。
另請參閱 registerAssetBundle()。
資源管理器。預設為 "assetManager" 應用程式元件。
方法詳細資訊
定義於: yii\base\Component::__call()
呼叫未定義為類別方法的具名方法。
此方法將檢查是否有任何附加的行為具有具名方法,並在可用時執行它。
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當調用未知方法時,它會被隱式呼叫。
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法傳回值 |
---|---|---|
throws | yii\base\UnknownMethodException |
當呼叫未知方法時 |
public function __call($name, $params)
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $object) {
if ($object->hasMethod($name)) {
return call_user_func_array([$object, $name], $params);
}
}
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
public void __clone ( ) |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
定義於: yii\base\BaseObject::__construct()
建構子。
預設實作會執行兩件事
- 使用給定的配置
$config
初始化物件。 - 呼叫 init()。
如果在子類別中覆寫此方法,建議
- 建構子的最後一個參數是配置陣列,例如此處的
$config
。 - 在建構子的結尾呼叫父類別實作。
public void __construct ( $config = [] ) | ||
$config | array |
將用於初始化物件屬性的名稱-值對 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義於: yii\base\Component::__get()
傳回元件屬性的值。
此方法將依以下順序檢查並採取相應措施
- getter 定義的屬性:傳回 getter 結果
- 行為的屬性:傳回行為屬性值
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $value = $component->property;
時,它會被隱式呼叫。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | string |
屬性名稱 |
return | mixed |
屬性值或行為的屬性值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
throws | yii\base\InvalidCallException |
如果屬性為唯寫。 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
// read property, e.g. getName()
return $this->$getter();
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\Component::__isset()
檢查屬性是否已設定,即已定義且非 null。
此方法將依以下順序檢查並採取相應措施
- setter 定義的屬性:傳回屬性是否已設定
- 行為的屬性:傳回屬性是否已設定
- 針對不存在的屬性傳回
false
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property)
時,它會被隱式呼叫。
public boolean __isset ( $name ) | ||
$name | string |
屬性名稱或事件名稱 |
return | boolean |
具名屬性是否已設定 |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name !== null;
}
}
return false;
}
定義於: yii\base\Component::__set()
設定元件屬性的值。
此方法將依以下順序檢查並採取相應措施
- setter 定義的屬性:設定屬性值
- 格式為 "on xyz" 的事件:將處理常式附加到事件 "xyz"
- 格式為 "as xyz" 的行為:附加名為 "xyz" 的行為
- 行為的屬性:設定行為屬性值
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $component->property = $value;
時,它會被隱式呼叫。
另請參閱 __get()。
public void __set ( $name, $value ) | ||
$name | string |
屬性名稱或事件名稱 |
$value | mixed |
屬性值 |
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
---|---|---|
throws | yii\base\InvalidCallException |
如果屬性為唯讀。 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
// set property
$this->$setter($value);
return;
} elseif (strncmp($name, 'on ', 3) === 0) {
// on event: attach event handler
$this->on(trim(substr($name, 3)), $value);
return;
} elseif (strncmp($name, 'as ', 3) === 0) {
// as behavior: attach behavior
$name = trim(substr($name, 3));
$this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = $value;
return;
}
}
if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\Component::__unset()
將元件屬性設定為 null。
此方法將依以下順序檢查並採取相應措施
- setter 定義的屬性:將屬性值設定為 null
- 行為的屬性:將屬性值設定為 null
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 unset($component->property)
時,它會被隱式呼叫。
public void __unset ( $name ) | ||
$name | string |
屬性名稱 |
throws | yii\base\InvalidCallException |
如果屬性為唯讀。 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = null;
return;
}
}
throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}
public void addDynamicPlaceholder ( $placeholder, $statements ) | ||
$placeholder | ||
$statements | string |
用於產生動態內容的 PHP 語句。 |
public function addDynamicPlaceholder($placeholder, $statements)
{
foreach ($this->cacheStack as $cache) {
if ($cache instanceof DynamicContentAwareInterface) {
$cache->addDynamicPlaceholder($placeholder, $statements);
} else {
// TODO: Remove in 2.1
$cache->dynamicPlaceholders[$placeholder] = $statements;
}
}
$this->dynamicPlaceholders[$placeholder] = $statements;
}
定義於: yii\base\View::afterRender()
此方法在 renderFile() 渲染視圖檔案後立即調用。
預設實作將觸發 EVENT_AFTER_RENDER 事件。如果您覆寫此方法,請確保先呼叫父類別實作。
public void afterRender ( $viewFile, $params, &$output ) | ||
$viewFile | string |
正在渲染的視圖檔案。 |
$params | array |
傳遞給 render() 方法的參數陣列。 |
$output | string |
視圖檔案的渲染結果。<0xE6><0x9B><0xB4><0xE6><0x9B><0xB4>對此參數的更新將被傳遞回去,並由 renderFile() 傳回。 |
public function afterRender($viewFile, $params, &$output)
{
if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {
$event = new ViewEvent([
'viewFile' => $viewFile,
'params' => $params,
]);
$event->output =& $output;
$this->trigger(self::EVENT_AFTER_RENDER, $event);
}
}
定義於: yii\base\Component::attachBehavior()
將行為附加到此元件。
此方法將會根據給定的配置建立行為物件。之後,將會呼叫 yii\base\Behavior::attach() 方法,將行為物件附加到此組件。
另請參閱 detachBehavior()。
public yii\base\Behavior attachBehavior ( $name, $behavior ) | ||
$name | string |
行為的名稱。 |
$behavior | string|array|yii\base\Behavior |
行為配置。可以是下列其中一種
|
return | yii\base\Behavior |
行為物件 |
---|
public function attachBehavior($name, $behavior)
{
$this->ensureBehaviors();
return $this->attachBehaviorInternal($name, $behavior);
}
定義於: yii\base\Component::attachBehaviors()
將行為列表附加到元件。
每個行為都以其名稱索引,並且應該是一個 yii\base\Behavior 物件、一個指定行為類別的字串,或是一個用於建立行為的配置陣列。
另請參閱 attachBehavior()。
public void attachBehaviors ( $behaviors ) | ||
$behaviors | array |
要附加到組件的行為列表 |
public function attachBehaviors($behaviors)
{
$this->ensureBehaviors();
foreach ($behaviors as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
定義於: yii\base\View::beforeRender()
此方法在 renderFile() 渲染視圖檔案之前立即調用。
預設實作將會觸發 EVENT_BEFORE_RENDER 事件。如果您覆寫此方法,請確保您先呼叫父類別的實作。
public boolean beforeRender ( $viewFile, $params ) | ||
$viewFile | string |
要渲染的視圖檔案。 |
$params | array |
傳遞給 render() 方法的參數陣列。 |
return | boolean |
是否繼續渲染視圖檔案。 |
---|
public function beforeRender($viewFile, $params)
{
$event = new ViewEvent([
'viewFile' => $viewFile,
'params' => $params,
]);
$this->trigger(self::EVENT_BEFORE_RENDER, $event);
return $event->isValid;
}
public yii\widgets\Block beginBlock ( $id, $renderInPlace = false ) | ||
$id | string |
區塊 ID。 |
$renderInPlace | boolean |
是否就地渲染區塊內容。預設為 false,表示捕獲的區塊將不會顯示。 |
return | yii\widgets\Block |
Block 小部件實例 |
---|
public function beginBlock($id, $renderInPlace = false)
{
return Block::begin([
'id' => $id,
'renderInPlace' => $renderInPlace,
'view' => $this,
]);
}
標記 HTML body 區段的開始。
public void beginBody ( ) |
public function beginBody()
{
echo self::PH_BODY_BEGIN;
$this->trigger(self::EVENT_BEGIN_BODY);
}
定義於: yii\base\View::beginCache()
開始片段快取。
如果快取內容可用,此方法將會顯示快取內容。否則,它將開始快取,並預期呼叫 endCache() 以結束快取並將內容儲存到快取中。片段快取的典型用法如下:
if ($this->beginCache($id)) {
// ...generate content here
$this->endCache();
}
public boolean beginCache ( $id, $properties = [] ) | ||
$id | string |
唯一 ID,用於識別要快取的片段。 |
$properties | array |
yii\widgets\FragmentCache 的初始屬性值 |
return | boolean |
是否應產生用於快取的內容。如果快取版本可用,則為 False。 |
---|
public function beginCache($id, $properties = [])
{
$properties['id'] = $id;
$properties['view'] = $this;
/* @var $cache FragmentCache */
$cache = FragmentCache::begin($properties);
if ($cache->getCachedContent() !== false) {
$this->endCache();
return false;
}
return true;
}
定義於: yii\base\View::beginContent()
開始渲染將由指定的視圖裝飾的內容。
此方法可用於實作巢狀佈局。例如,佈局可以嵌入在另一個佈局檔案中,該檔案指定為 '@app/views/layouts/base.php',如下所示
<?php $this->beginContent('@app/views/layouts/base.php'); ?>
//...layout content here...
<?php $this->endContent(); ?>
public yii\widgets\ContentDecorator beginContent ( $viewFile, $params = [] ) | ||
$viewFile | string |
將用於裝飾此小部件封閉內容的視圖檔案。這可以指定為視圖檔案路徑或 路徑別名。 |
$params | array |
要在裝飾性視圖中提取並可用的變數(name => value)。 |
return | yii\widgets\ContentDecorator |
ContentDecorator 小部件實例 |
---|
public function beginContent($viewFile, $params = [])
{
return ContentDecorator::begin([
'viewFile' => $viewFile,
'params' => $params,
'view' => $this,
]);
}
定義於: yii\base\View::beginPage()
標記頁面的開始。
public void beginPage ( ) |
public function beginPage()
{
ob_start();
ob_implicit_flush(false);
$this->trigger(self::EVENT_BEGIN_PAGE);
}
定義於: yii\base\Component::behaviors()
傳回此元件應表現為的行為列表。
子類別可以覆寫此方法,以指定它們想要表現為的行為。
此方法的傳回值應該是一個行為物件或配置的陣列,以行為名稱索引。行為配置可以是指定行為類別的字串,或具有以下結構的陣列
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
請注意,行為類別必須從 yii\base\Behavior 擴展。可以使用名稱或匿名方式附加行為。當名稱用作陣列鍵時,使用此名稱,稍後可以使用 getBehavior() 檢索行為,或使用 detachBehavior() 分離行為。匿名行為無法檢索或分離。
在此方法中宣告的行為將會自動(按需)附加到組件。
public array behaviors ( ) | ||
return | array |
行為配置。 |
---|
public function behaviors()
{
return [];
}
定義於: yii\base\Component::canGetProperty()
傳回一個值,指示是否可以讀取屬性。
如果滿足以下條件,則可以讀取屬性:
- 類別具有與指定名稱關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為具有給定名稱的可讀屬性(當
$checkBehaviors
為 true 時)。
另請參閱 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為的屬性視為此組件的屬性 |
return | boolean |
屬性是否可以讀取 |
---|
public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
定義於: yii\base\Component::canSetProperty()
傳回一個值,指示是否可以設定屬性。
如果滿足以下條件,則可以寫入屬性:
- 類別具有與指定名稱關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為具有給定名稱的可寫屬性(當
$checkBehaviors
為 true 時)。
另請參閱 canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為的屬性視為此組件的屬性 |
return | boolean |
屬性是否可以寫入 |
---|
public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
::class
。
定義於: yii\base\BaseObject::className()
傳回此類別的完整限定名稱。
public static string className ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
清除已註冊的 meta 標籤、連結標籤、CSS/JS 腳本和檔案。
public void clear ( ) |
public function clear()
{
$this->metaTags = [];
$this->linkTags = [];
$this->css = [];
$this->cssFiles = [];
$this->js = [];
$this->jsFiles = [];
$this->assetBundles = [];
}
public yii\base\Behavior|null detachBehavior ( $name ) | ||
$name | string |
行為的名稱。 |
return | yii\base\Behavior|null |
分離的行為。如果行為不存在,則為 Null。 |
---|
public function detachBehavior($name)
{
$this->ensureBehaviors();
if (isset($this->_behaviors[$name])) {
$behavior = $this->_behaviors[$name];
unset($this->_behaviors[$name]);
$behavior->detach();
return $behavior;
}
return null;
}
定義於: yii\base\Component::detachBehaviors()
從元件分離所有行為。
public void detachBehaviors ( ) |
public function detachBehaviors()
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $name => $behavior) {
$this->detachBehavior($name);
}
}
定義於: yii\base\View::endBlock()
結束記錄區塊。
public void endBlock ( ) |
public function endBlock()
{
Block::end();
}
標記 HTML body 區段的結束。
public void endBody ( ) |
public function endBody()
{
$this->trigger(self::EVENT_END_BODY);
echo self::PH_BODY_END;
foreach (array_keys($this->assetBundles) as $bundle) {
$this->registerAssetFiles($bundle);
}
}
定義於: yii\base\View::endCache()
結束片段快取。
public void endCache ( ) |
public function endCache()
{
FragmentCache::end();
}
定義於: yii\base\View::endContent()
結束內容渲染。
public void endContent ( ) |
public function endContent()
{
ContentDecorator::end();
}
標記 HTML 頁面的結束。
public void endPage ( $ajaxMode = false ) | ||
$ajaxMode | boolean |
視圖是否在 AJAX 模式下渲染。如果為 true,則在 POS_READY 和 POS_LOAD 位置註冊的 JS 腳本將像普通腳本一樣在視圖末尾渲染。 |
public function endPage($ajaxMode = false)
{
$this->trigger(self::EVENT_END_PAGE);
$this->isPageEnded = true;
$content = ob_get_clean();
echo strtr($content, [
self::PH_HEAD => $this->renderHeadHtml(),
self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
]);
$this->clear();
}
定義於: yii\base\Component::ensureBehaviors()
確保在 behaviors() 中宣告的行為已附加到此元件。
public void ensureBehaviors ( ) |
public function ensureBehaviors()
{
if ($this->_behaviors === null) {
$this->_behaviors = [];
foreach ($this->behaviors() as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
}
public mixed evaluateDynamicContent ( $statements ) | ||
$statements | string |
要評估的 PHP 語句。 |
return | mixed |
PHP 語句的傳回值。 |
---|
public function evaluateDynamicContent($statements)
{
return eval($statements);
}
定義於: yii\base\View::findViewFile()
根據給定的視圖名稱尋找視圖檔案。
protected string findViewFile ( $view, $context = null ) | ||
$view | string | |
$context | object|null |
要分配給視圖的上下文,稍後可以通過視圖中的 $context 訪問。如果上下文實作 yii\base\ViewContextInterface,它也可以用於定位與相對視圖名稱對應的視圖檔案。 |
return | string |
視圖檔案路徑。請注意,該檔案可能不存在。 |
---|---|---|
throws | yii\base\InvalidCallException |
如果在沒有活動上下文來確定相應視圖檔案的情況下給定相對視圖名稱。 |
protected function findViewFile($view, $context = null)
{
if (strncmp($view, '@', 1) === 0) {
// e.g. "@app/views/main"
$file = Yii::getAlias($view);
} elseif (strncmp($view, '//', 2) === 0) {
// e.g. "//layouts/main"
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} elseif (strncmp($view, '/', 1) === 0) {
// e.g. "/site/index"
if (Yii::$app->controller !== null) {
$file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} else {
throw new InvalidCallException("Unable to locate view file for view '$view': no active controller.");
}
} elseif ($context instanceof ViewContextInterface) {
$file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
} elseif (($currentViewFile = $this->getRequestedViewFile()) !== false) {
$file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view;
} else {
throw new InvalidCallException("Unable to resolve view file for view '$view': no active view context.");
}
if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
return $file;
}
$path = $file . '.' . $this->defaultExtension;
if ($this->defaultExtension !== 'php' && !is_file($path)) {
$path = $file . '.php';
}
return $path;
}
註冊此視圖物件使用的資源管理器。
public yii\web\AssetManager getAssetManager ( ) | ||
return | yii\web\AssetManager |
資源管理器。預設為 "assetManager" 應用程式元件。 |
---|
public function getAssetManager()
{
return $this->_assetManager ?: Yii::$app->getAssetManager();
}
定義於: yii\base\Component::getBehavior()
傳回具名的行為物件。
public yii\base\Behavior|null getBehavior ( $name ) | ||
$name | string |
行為名稱 |
return | yii\base\Behavior|null |
行為物件;如果行為不存在,則為 null |
---|
public function getBehavior($name)
{
$this->ensureBehaviors();
return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}
定義於: yii\base\Component::getBehaviors()
傳回附加到此元件的所有行為。
public yii\base\Behavior[] getBehaviors ( ) | ||
return | yii\base\Behavior[] |
附加到此組件的行為列表 |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
定義於: yii\base\View::getDynamicContents()
傳回目前作用中的動態內容類別實例的列表。
public yii\base\DynamicContentAwareInterface[] getDynamicContents ( ) | ||
return | yii\base\DynamicContentAwareInterface[] |
支援動態內容的類別實例。 |
---|
public function getDynamicContents()
{
return $this->cacheStack;
}
定義於: yii\base\View::getDynamicPlaceholders()
傳回動態內容的預留位置列表。此方法在內部用於實作內容快取功能。
public array getDynamicPlaceholders ( ) | ||
return | array |
佔位符的列表。 |
---|
public function getDynamicPlaceholders()
{
return $this->dynamicPlaceholders;
}
protected string|boolean getRequestedViewFile ( ) | ||
return | string|boolean |
目前正在渲染的請求視圖。如果沒有正在渲染的視圖檔案,則為 False。 |
---|
protected function getRequestedViewFile()
{
return empty($this->_viewFiles) ? false : end($this->_viewFiles)['requested'];
}
public string|boolean getViewFile ( ) | ||
return | string|boolean |
目前正在渲染的視圖檔案。如果沒有正在渲染的視圖檔案,則為 False。 |
---|
public function getViewFile()
{
return empty($this->_viewFiles) ? false : end($this->_viewFiles)['resolved'];
}
定義於: yii\base\Component::hasEventHandlers()
傳回一個值,指示是否有名稱事件附加任何處理常式。
public boolean hasEventHandlers ( $name ) | ||
$name | string |
事件名稱 |
return | boolean |
是否有任何處理程序附加到事件。 |
---|
public function hasEventHandlers($name)
{
$this->ensureBehaviors();
if (!empty($this->_events[$name])) {
return true;
}
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
return true;
}
}
return Event::hasHandlers($this, $name);
}
Defined in: yii\base\Component::hasMethod()
傳回一個值,指示是否已定義方法。
如果滿足以下條件,則方法被定義:
- 類別具有指定名稱的方法
- 附加的行為具有給定名稱的方法 (當
$checkBehaviors
為 true 時)。
public boolean hasMethod ( $name, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkBehaviors | boolean |
是否將行為的方法視為此組件的方法 |
return | boolean |
方法是否被定義 |
---|
public function hasMethod($name, $checkBehaviors = true)
{
if (method_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->hasMethod($name)) {
return true;
}
}
}
return false;
}
Defined in: yii\base\Component::hasProperty()
傳回一個值,指示是否為此元件定義了屬性。
如果滿足以下條件,則屬性被定義:
- 類別具有與指定名稱相關聯的 getter 或 setter 方法 (在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為具有給定名稱的屬性 (當
$checkBehaviors
為 true 時)。
參見
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為的屬性視為此組件的屬性 |
return | boolean |
屬性是否被定義 |
---|
public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}
Defined in: yii\base\View::init()
初始化視圖元件。
public void init ( ) |
public function init()
{
parent::init();
if (is_array($this->theme)) {
if (!isset($this->theme['class'])) {
$this->theme['class'] = 'yii\base\Theme';
}
$this->theme = Yii::createObject($this->theme);
} elseif (is_string($this->theme)) {
$this->theme = Yii::createObject($this->theme);
}
}
Defined in: yii\base\Component::off()
從此元件分離現有的事件處理常式。
此方法與 on() 相反。
注意:如果為事件名稱傳遞了萬用字元模式,則只會移除使用此萬用字元註冊的處理程序,而使用與此萬用字元匹配的純名稱註冊的處理程序將會保留。
參見 on()。
public boolean off ( $name, $handler = null ) | ||
$name | string |
事件名稱 |
$handler | callable|null |
要移除的事件處理程序。 如果為 null,則將移除附加到命名事件的所有處理程序。 |
return | boolean |
如果找到並分離處理程序 |
---|
public function off($name, $handler = null)
{
$this->ensureBehaviors();
if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
return false;
}
if ($handler === null) {
unset($this->_events[$name], $this->_eventWildcards[$name]);
return true;
}
$removed = false;
// plain event names
if (isset($this->_events[$name])) {
foreach ($this->_events[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_events[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_events[$name] = array_values($this->_events[$name]);
return true;
}
}
// wildcard event names
if (isset($this->_eventWildcards[$name])) {
foreach ($this->_eventWildcards[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_eventWildcards[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
// remove empty wildcards to save future redundant regex checks:
if (empty($this->_eventWildcards[$name])) {
unset($this->_eventWildcards[$name]);
}
}
}
return $removed;
}
Defined in: yii\base\Component::on()
將事件處理常式附加到事件。
事件處理程序必須是有效的 PHP 回調。 以下是一些範例
function ($event) { ... } // anonymous function
[$object, 'handleClick'] // $object->handleClick()
['Page', 'handleClick'] // Page::handleClick()
'handleClick' // global function handleClick()
事件處理程序必須使用以下簽名定義:
function ($event)
其中 $event
是一個 yii\base\Event 物件,其中包含與事件相關聯的參數。
自 2.0.14 版本起,您可以將事件名稱指定為萬用字元模式
$component->on('event.group.*', function ($event) {
Yii::trace($event->name . ' is triggered.');
});
參見 off()。
public void on ( $name, $handler, $data = null, $append = true ) | ||
$name | string |
事件名稱 |
$handler | callable |
事件處理程序 |
$data | mixed |
事件觸發時要傳遞給事件處理程序的資料。 當調用事件處理程序時,可以透過 yii\base\Event::$data 存取此資料。 |
$append | boolean |
是否將新的事件處理程序附加到現有處理程序列表的末尾。 如果為 false,則新的處理程序將插入到現有處理程序列表的開頭。 |
public function on($name, $handler, $data = null, $append = true)
{
$this->ensureBehaviors();
if (strpos($name, '*') !== false) {
if ($append || empty($this->_eventWildcards[$name])) {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift($this->_eventWildcards[$name], [$handler, $data]);
}
return;
}
if ($append || empty($this->_events[$name])) {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift($this->_events[$name], [$handler, $data]);
}
}
Defined in: yii\base\View::popDynamicContent()
從目前作用中的動態內容類別實例列表中移除最後一個支援動態內容的類別實例。
public void popDynamicContent ( ) |
public function popDynamicContent()
{
array_pop($this->cacheStack);
}
Defined in: yii\base\View::pushDynamicContent()
將支援動態內容的類別實例新增到目前作用中的動態內容類別實例列表的末尾。
public void pushDynamicContent ( yii\base\DynamicContentAwareInterface $instance ) | ||
$instance | yii\base\DynamicContentAwareInterface |
支援動態內容的類別實例。 |
public function pushDynamicContent(DynamicContentAwareInterface $instance)
{
$this->cacheStack[] = $instance;
}
註冊具名的資源包。
所有相依的資源包將被註冊。
public yii\web\AssetBundle registerAssetBundle ( $name, $position = null ) | ||
$name | string |
資源包的類別名稱 (不帶前導反斜線) |
$position | integer|null |
如果設定,這將強制 JavaScript 檔案的最小位置。 這將根據相依資源的 JavaScript 檔案位置進行調整,如果無法滿足要求則會失敗。 如果此值為 null,則資源包的位置設定將不會更改。 有關 JavaScript 位置的更多詳細資訊,請參閱 registerJsFile()。 |
return | yii\web\AssetBundle |
已註冊的資源包實例 |
---|---|---|
throws | yii\base\InvalidConfigException |
如果資源包不存在或偵測到循環依賴關係 |
public function registerAssetBundle($name, $position = null)
{
if (!isset($this->assetBundles[$name])) {
$am = $this->getAssetManager();
$bundle = $am->getBundle($name);
$this->assetBundles[$name] = false;
// register dependencies
$pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
foreach ($bundle->depends as $dep) {
$this->registerAssetBundle($dep, $pos);
}
$this->assetBundles[$name] = $bundle;
} elseif ($this->assetBundles[$name] === false) {
throw new InvalidConfigException("A circular dependency is detected for bundle '$name'.");
} else {
$bundle = $this->assetBundles[$name];
}
if ($position !== null) {
$pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
if ($pos === null) {
$bundle->jsOptions['position'] = $pos = $position;
} elseif ($pos > $position) {
throw new InvalidConfigException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'.");
}
// update position for all dependencies
foreach ($bundle->depends as $dep) {
$this->registerAssetBundle($dep, $pos);
}
}
return $bundle;
}
註冊資源包提供的所有檔案,包括相依資源包檔案。
一旦檔案註冊,即從 $assetBundles 中移除資源包。
protected void registerAssetFiles ( $name ) | ||
$name | string |
要註冊的資源包名稱 |
protected function registerAssetFiles($name)
{
if (!isset($this->assetBundles[$name])) {
return;
}
$bundle = $this->assetBundles[$name];
if ($bundle) {
foreach ($bundle->depends as $dep) {
$this->registerAssetFiles($dep);
}
$bundle->registerAssetFiles($this);
}
unset($this->assetBundles[$name]);
}
註冊 CSRF meta 標籤。
它們被動態渲染以檢索每個請求的新 CSRF 令牌。
$view->registerCsrfMetaTags();
上面的程式碼將產生 <meta name="csrf-param" content="[yii\web\Request::$csrfParam]">
和 <meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">
添加到頁面。
注意:ActiveForm 的隱藏 CSRF 輸入將透過從 yii.js
調用 window.yii.refreshCsrfToken()
自動刷新。
public void registerCsrfMetaTags ( ) |
public function registerCsrfMetaTags()
{
$this->metaTags['csrf_meta_tags'] = $this->renderDynamic('return yii\helpers\Html::csrfMetaTags();');
}
註冊 CSS 程式碼區塊。
public void registerCss ( $css, $options = [], $key = null ) | ||
$css | string |
要註冊的 CSS 程式碼區塊的內容 |
$options | array |
|
$key | string|null |
標識 CSS 程式碼區塊的鍵。 如果為 null,它將使用 $css 作為鍵。 如果使用相同的鍵註冊了兩個 CSS 程式碼區塊,則後者將覆蓋前者。 |
public function registerCss($css, $options = [], $key = null)
{
$key = $key ?: md5($css);
$this->css[$key] = Html::style($css, $options);
}
註冊 CSS 檔案。
此方法應用於簡單註冊 CSS 檔案。 如果您想使用 yii\web\AssetManager 的功能,例如將時間戳記附加到 URL 和檔案發佈選項,請改用 yii\web\AssetBundle 和 registerAssetBundle()。
public void registerCssFile ( $url, $options = [], $key = null ) | ||
$url | string |
要註冊的 CSS 檔案。 |
$options | array |
連結標籤的 HTML 屬性。 有關支援的選項,請參閱 yii\helpers\Html::cssFile()。 以下選項經過特殊處理,不被視為 HTML 屬性
|
$key | string|null |
標識 CSS 腳本檔案的鍵。 如果為 null,它將使用 $url 作為鍵。 如果使用相同的鍵註冊了兩個 CSS 檔案,則後者將覆蓋前者。 |
throws | yii\base\InvalidConfigException |
---|
public function registerCssFile($url, $options = [], $key = null)
{
$this->registerFile('css', $url, $options, $key);
}
註冊 JS 程式碼區塊。
public void registerJs ( $js, $position = self::POS_READY, $key = null ) | ||
$js | string |
要註冊的 JS 程式碼區塊 |
$position | integer |
JS 腳本標籤應插入在頁面中的位置。 可能的值為 |
$key | string|null |
標識 JS 程式碼區塊的鍵。 如果為 null,它將使用 $js 作為鍵。 如果使用相同的鍵註冊了兩個 JS 程式碼區塊,則後者將覆蓋前者。 |
public function registerJs($js, $position = self::POS_READY, $key = null)
{
$key = $key ?: md5($js);
$this->js[$position][$key] = $js;
if ($position === self::POS_READY || $position === self::POS_LOAD) {
JqueryAsset::register($this);
}
}
註冊 JS 檔案。
此方法應用於簡單註冊 JS 檔案。 如果您想使用 yii\web\AssetManager 的功能,例如將時間戳記附加到 URL 和檔案發佈選項,請改用 yii\web\AssetBundle 和 registerAssetBundle()。
public void registerJsFile ( $url, $options = [], $key = null ) | ||
$url | string |
要註冊的 JS 檔案。 |
$options | array |
腳本標籤的 HTML 屬性。 以下選項經過特殊處理,不被視為 HTML 屬性
有關其他支援的選項,請參閱 yii\helpers\Html::jsFile()。 |
$key | string|null |
標識 JS 腳本檔案的鍵。 如果為 null,它將使用 $url 作為鍵。 如果使用相同的鍵在相同位置註冊了兩個 JS 檔案,則後者將覆蓋前者。 請注意,position 選項優先,因此使用相同鍵但不同 position 選項註冊的檔案將不會互相覆蓋。 |
throws | yii\base\InvalidConfigException |
---|
public function registerJsFile($url, $options = [], $key = null)
{
$this->registerFile('js', $url, $options, $key);
}
註冊定義變數的 JS 程式碼區塊。變數名稱將用作鍵,以防止重複的變數名稱。
public void registerJsVar ( $name, $value, $position = self::POS_HEAD ) | ||
$name | string |
變數名稱 |
$value | array|string |
變數的值 |
$position | integer |
JavaScript 變數應插入在頁面中的位置。 可能的值為 |
public function registerJsVar($name, $value, $position = self::POS_HEAD)
{
$js = sprintf('var %s = %s;', $name, \yii\helpers\Json::htmlEncode($value));
$this->registerJs($js, $position, $name);
}
註冊連結標籤。
例如,可以像下面這樣添加自訂 favicon 的連結標籤
$view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']);
這將產生以下 HTML:<link rel="icon" type="image/png" href="/myicon.png">
。
注意: 要註冊 CSS 樣式表的連結標籤,請改用 registerCssFile(),它為此類型的連結標籤提供了更多選項。
public void registerLinkTag ( $options, $key = null ) | ||
$options | array |
連結標籤的 HTML 屬性。 |
$key | string|null |
標識連結標籤的鍵。 如果使用相同的鍵註冊了兩個連結標籤,則後者將覆蓋前者。 如果此值為 null,則新的連結標籤將附加到現有的標籤。 |
public function registerLinkTag($options, $key = null)
{
if ($key === null) {
$this->linkTags[] = Html::tag('link', '', $options);
} else {
$this->linkTags[$key] = Html::tag('link', '', $options);
}
}
註冊 meta 標籤。
例如,可以像下面這樣添加描述 meta 標籤
$view->registerMetaTag([
'name' => 'description',
'content' => 'This website is about funny raccoons.'
]);
將產生 meta 標籤 <meta name="description" content="This website is about funny raccoons.">
。
public void registerMetaTag ( $options, $key = null ) | ||
$options | array |
meta 標籤的 HTML 屬性。 |
$key | string|null |
標識 meta 標籤的鍵。 如果使用相同的鍵註冊了兩個 meta 標籤,則後者將覆蓋前者。 如果此值為 null,則新的 meta 標籤將附加到現有的標籤。 |
public function registerMetaTag($options, $key = null)
{
if ($key === null) {
$this->metaTags[] = Html::tag('meta', '', $options);
} else {
$this->metaTags[$key] = Html::tag('meta', '', $options);
}
}
Defined in: yii\base\View::render()
渲染視圖。
要渲染的視圖可以使用以下格式之一指定
- 路徑別名 (例如 "@app/views/site/index");
- 應用程式內的絕對路徑 (例如 "//site/index"):視圖名稱以雙斜線開頭。 實際的視圖檔案將在應用程式的 視圖路徑 下查找。
- 當前模組內的絕對路徑 (例如 "/site/index"):視圖名稱以單斜線開頭。 實際的視圖檔案將在 當前模組 的 視圖路徑 下查找。
- 相對視圖 (例如 "index"):視圖名稱不以
@
或/
開頭。 相應的視圖檔案將在視圖$context
的 視圖路徑 下查找。 如果未給出$context
,則將在包含當前正在渲染的視圖的目錄下查找 (即,當在另一個視圖中渲染視圖時會發生這種情況)。
參見 renderFile()。
public string render ( $view, $params = [], $context = null ) | ||
$view | string |
視圖名稱。 |
$params | array |
將被提取並在視圖檔案中可用的參數 (名稱-值對)。 |
$context | object|null |
要分配給視圖的上下文,稍後可以通過視圖中的 $context 訪問。如果上下文實作 yii\base\ViewContextInterface,它也可以用於定位與相對視圖名稱對應的視圖檔案。 |
return | string |
渲染結果 |
---|---|---|
throws | yii\base\ViewNotFoundException |
如果視圖檔案不存在。 |
throws | yii\base\InvalidCallException |
如果視圖無法解析。 |
public function render($view, $params = [], $context = null)
{
$viewFile = $this->findViewFile($view, $context);
return $this->renderFile($viewFile, $params, $context);
}
回應 AJAX 請求渲染視圖。
此方法與 render() 類似,不同之處在於它將使用 beginPage()、head()、beginBody()、endBody() 和 endPage() 的調用包圍正在渲染的視圖。 這樣做,該方法能夠將使用視圖註冊的 JS/CSS 腳本和檔案注入到渲染結果中。
參見 render()。
public string renderAjax ( $view, $params = [], $context = null ) | ||
$view | string |
視圖名稱。 有關如何指定此參數,請參閱 render()。 |
$params | array |
將被提取並在視圖檔案中可用的參數 (名稱-值對)。 |
$context | object|null |
視圖應用於渲染視圖的上下文。 如果為 null,將使用現有的 $context。 |
return | string |
渲染結果 |
---|
public function renderAjax($view, $params = [], $context = null)
{
$viewFile = $this->findViewFile($view, $context);
ob_start();
ob_implicit_flush(false);
$this->beginPage();
$this->head();
$this->beginBody();
echo $this->renderFile($viewFile, $params, $context);
$this->endBody();
$this->endPage(true);
return ob_get_clean();
}
渲染要插入到 body 區段開頭的內容。
內容使用已註冊的 JS 程式碼區塊和檔案進行渲染。
protected string renderBodyBeginHtml ( ) | ||
return | string |
渲染的內容 |
---|
protected function renderBodyBeginHtml()
{
$lines = [];
if (!empty($this->jsFiles[self::POS_BEGIN])) {
$lines[] = implode("\n", $this->jsFiles[self::POS_BEGIN]);
}
if (!empty($this->js[self::POS_BEGIN])) {
$lines[] = Html::script(implode("\n", $this->js[self::POS_BEGIN]));
}
return empty($lines) ? '' : implode("\n", $lines);
}
渲染要插入到 body 區段結尾的內容。
內容使用已註冊的 JS 程式碼區塊和檔案進行渲染。
protected string renderBodyEndHtml ( $ajaxMode ) | ||
$ajaxMode | boolean |
視圖是否在 AJAX 模式下渲染。如果為 true,則在 POS_READY 和 POS_LOAD 位置註冊的 JS 腳本將像普通腳本一樣在視圖末尾渲染。 |
return | string |
渲染的內容 |
---|
protected function renderBodyEndHtml($ajaxMode)
{
$lines = [];
if (!empty($this->jsFiles[self::POS_END])) {
$lines[] = implode("\n", $this->jsFiles[self::POS_END]);
}
if ($ajaxMode) {
$scripts = [];
if (!empty($this->js[self::POS_END])) {
$scripts[] = implode("\n", $this->js[self::POS_END]);
}
if (!empty($this->js[self::POS_READY])) {
$scripts[] = implode("\n", $this->js[self::POS_READY]);
}
if (!empty($this->js[self::POS_LOAD])) {
$scripts[] = implode("\n", $this->js[self::POS_LOAD]);
}
if (!empty($scripts)) {
$lines[] = Html::script(implode("\n", $scripts));
}
} else {
if (!empty($this->js[self::POS_END])) {
$lines[] = Html::script(implode("\n", $this->js[self::POS_END]));
}
if (!empty($this->js[self::POS_READY])) {
$js = "jQuery(function ($) {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});";
$lines[] = Html::script($js);
}
if (!empty($this->js[self::POS_LOAD])) {
$js = "jQuery(window).on('load', function () {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});";
$lines[] = Html::script($js);
}
}
return empty($lines) ? '' : implode("\n", $lines);
}
Defined in: yii\base\View::renderDynamic()
渲染給定 PHP 語句傳回的動態內容。
當內容的某些部分 (稱為動態內容) 不應被快取時,此方法主要與內容快取 (片段快取和頁面快取) 一起使用。 動態內容必須由某些 PHP 語句返回。
public string renderDynamic ( $statements ) | ||
$statements | string |
用於產生動態內容的 PHP 語句。 |
return | string |
動態內容的佔位符,或者如果目前沒有活動的內容快取,則為動態內容。 請注意,大多數間接修改佈局的方法 (例如 registerJS() 或 registerJSFile()) 不適用於動態渲染。 |
---|
public function renderDynamic($statements)
{
if (!empty($this->cacheStack)) {
$n = count($this->dynamicPlaceholders);
$placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";
$this->addDynamicPlaceholder($placeholder, $statements);
return $placeholder;
}
return $this->evaluateDynamicContent($statements);
}
Defined in: yii\base\View::renderFile()
渲染視圖檔案。
如果啟用 $theme (非 null),它將嘗試渲染視圖檔案的主題版本 (如果可用)。
該方法將調用 yii\helpers\FileHelper::localize() 來本地化視圖檔案。
如果啟用 renderer (非 null),該方法將使用它來渲染視圖檔案。 否則,它將只會包含視圖檔案作為正常的 PHP 檔案,捕獲其輸出並將其作為字串返回。
public string renderFile ( $viewFile, $params = [], $context = null ) | ||
$viewFile | string |
視圖檔案。 這可以是絕對檔案路徑或其別名。 |
$params | array |
將被提取並在視圖檔案中可用的參數 (名稱-值對)。 |
$context | object|null |
視圖應用於渲染視圖的上下文。 如果為 null,將使用現有的 $context。 |
return | string |
渲染結果 |
---|---|---|
throws | yii\base\ViewNotFoundException |
如果視圖檔案不存在 |
public function renderFile($viewFile, $params = [], $context = null)
{
$viewFile = $requestedFile = Yii::getAlias($viewFile);
if ($this->theme !== null) {
$viewFile = $this->theme->applyTo($viewFile);
}
if (is_file($viewFile)) {
$viewFile = FileHelper::localize($viewFile);
} else {
throw new ViewNotFoundException("The view file does not exist: $viewFile");
}
$oldContext = $this->context;
if ($context !== null) {
$this->context = $context;
}
$output = '';
$this->_viewFiles[] = [
'resolved' => $viewFile,
'requested' => $requestedFile
];
if ($this->beforeRender($viewFile, $params)) {
Yii::debug("Rendering view file: $viewFile", __METHOD__);
$ext = pathinfo($viewFile, PATHINFO_EXTENSION);
if (isset($this->renderers[$ext])) {
if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {
$this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
}
/* @var $renderer ViewRenderer */
$renderer = $this->renderers[$ext];
$output = $renderer->render($this, $viewFile, $params);
} else {
$output = $this->renderPhpFile($viewFile, $params);
}
$this->afterRender($viewFile, $params, $output);
}
array_pop($this->_viewFiles);
$this->context = $oldContext;
return $output;
}
渲染要插入到 head 區段的內容。
內容會使用已註冊的 meta 標籤、link 標籤、CSS/JS 程式碼區塊和檔案進行呈現。
protected string renderHeadHtml ( ) | ||
return | string |
渲染的內容 |
---|
protected function renderHeadHtml()
{
$lines = [];
if (!empty($this->metaTags)) {
$lines[] = implode("\n", $this->metaTags);
}
if (!empty($this->linkTags)) {
$lines[] = implode("\n", $this->linkTags);
}
if (!empty($this->cssFiles)) {
$lines[] = implode("\n", $this->cssFiles);
}
if (!empty($this->css)) {
$lines[] = implode("\n", $this->css);
}
if (!empty($this->jsFiles[self::POS_HEAD])) {
$lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]);
}
if (!empty($this->js[self::POS_HEAD])) {
$lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD]));
}
return empty($lines) ? '' : implode("\n", $lines);
}
定義於: yii\base\View::renderPhpFile()
將視圖檔案渲染為 PHP 腳本。
此方法將視圖檔案視為 PHP 腳本並包含該檔案。它提取給定的參數,並使它們在視圖檔案中可用。此方法捕獲包含的視圖檔案的輸出,並將其作為字串傳回。
此方法主要應由視圖呈現器或 renderFile() 呼叫。
public string renderPhpFile ( $_file_, $_params_ = [] ) | ||
$_file_ | string |
視圖檔案。 |
$_params_ | array |
將被提取並在視圖檔案中可用的參數 (名稱-值對)。 |
return | string |
渲染結果 |
---|---|---|
throws | Throwable |
public function renderPhpFile($_file_, $_params_ = [])
{
$_obInitialLevel_ = ob_get_level();
ob_start();
ob_implicit_flush(false);
extract($_params_, EXTR_OVERWRITE);
try {
require $_file_;
return ob_get_clean();
} catch (\Exception $e) {
while (ob_get_level() > $_obInitialLevel_) {
if (!@ob_end_clean()) {
ob_clean();
}
}
throw $e;
} catch (\Throwable $e) {
while (ob_get_level() > $_obInitialLevel_) {
if (!@ob_end_clean()) {
ob_clean();
}
}
throw $e;
}
}
設定資源管理器。
public void setAssetManager ( $value ) | ||
$value | yii\web\AssetManager |
資源管理器 |
public function setAssetManager($value)
{
$this->_assetManager = $value;
}
定義於: yii\base\View::setDynamicPlaceholders()
設定動態內容的預留位置列表。此方法在內部用於實作內容快取功能。
public void setDynamicPlaceholders ( $placeholders ) | ||
$placeholders | array |
佔位符的列表。 |
public function setDynamicPlaceholders($placeholders)
{
$this->dynamicPlaceholders = $placeholders;
}
public void trigger ( $name, yii\base\Event $event = null ) | ||
$name | string |
事件名稱 |
$event | yii\base\Event|null |
事件實例。如果未設定,將會建立預設的 yii\base\Event 物件。 |
public function trigger($name, Event $event = null)
{
$this->ensureBehaviors();
$eventHandlers = [];
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (StringHelper::matchWildcard($wildcard, $name)) {
$eventHandlers[] = $handlers;
}
}
if (!empty($this->_events[$name])) {
$eventHandlers[] = $this->_events[$name];
}
if (!empty($eventHandlers)) {
$eventHandlers = call_user_func_array('array_merge', $eventHandlers);
if ($event === null) {
$event = new Event();
}
if ($event->sender === null) {
$event->sender = $this;
}
$event->handled = false;
$event->name = $name;
foreach ($eventHandlers as $handler) {
$event->data = $handler[1];
call_user_func($handler[0], $event);
// stop further handling if the event is handled
if ($event->handled) {
return;
}
}
}
// invoke class-level attached handlers
Event::trigger($this, $name, $event);
}
事件詳情
由 beginBody() 觸發的事件。
由 endBody() 觸發的事件。
註冊 或 登入 以進行評論。