<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Yii2視圖 [TOC] ## 視圖分配`render()`和`renderPartial()`   `render()`和`renderPartial()`之間最大的區別就是:一個是渲染父模板,一個不渲染父模板。   其中`render()`輸出父模板的內容,將渲染的內容嵌入父模板;`renderPartial()`則不輸出父模板的內容。只對本次渲染的局部內容進行輸出。 ```php public function actionIndex() { return $this->renderPartial('index',$data); return $this->render('index',$data); } ```   Yii2默認自帶的父模板文件是`<project>/views/layouts/main.php`,我們可以在當前控制器中新增`public $layouts = 'home';`屬性將其修改為我們指定的父模板文件是`<project>/views/layouts/home.php`,如: ```php <?php namespace app\controllers; use yii\web\Controller; class HomeController extends Controller { public $layout = 'home'; public function actionIndex() { $str = 'Hello World!'; return $this->render('index',compact('str')); } } ``` ## 渲染視圖 ### 使用rander()等渲染view文件的幾種方式   在`yii\base\View中findViewFile()`定義匹配規則 1. `別名`開頭,路徑指定view文件:`@app/views/site/about(.php)` 2. `//`開頭,使用**app**目錄下的view:`//site/index` 3. `/`開頭,使用當前Module中的views:`/site/index` 4. 直接使用字符串(重要): 'index' ### 在控制器中渲染視圖 如`$this->render('about')` `Controller::render`會調用`View::render`方法 ### 在view中渲染視圖 使用的視圖是當前view所在的目錄,如在index.php中 ```php $this->render('error',['name'=>'name','message'=>'message'])?> ``` `error.php`文件是`about.php`所在目錄下面的`error.php` ### render、renderPartial、renderContent、renderAjax、renderFile 1. `render`顯示view和layout 2. `renderPartial`只顯示view 3. `renderContent`只渲染layout 4. `renderFile`顯示指定的文件,是最基礎的方法,其中`renderAjax`,`renderPartial`最終都是調用`renderFile` 5. `renderAjax`只顯示view,以ajax方式渲染頁面,可以配合js/css實現各種特效 ## 視圖文件的相互調用 有時我們需要引入其他模板的視圖布局,那么我們可以在模板文件中使用`render()`方法進行引用,如果要引入的視圖文件與當前視圖文件在一個文件夾下我們直接可以傳入視圖名稱,如果不是可以傳入`/控制器名/視圖名`,如下: ```html <?= $this->render('about'); ?> <?= $this->render('/site/about'); ?> ``` ## 視圖防止XSS攻擊 ```html <?php use yii\helpers\Html; use yii\helpers\HtmlPurifier; ?> <p><?= $str; // 輸出hello world! 彈出alert ?></p> <p><?= Html::encode($str); // 輸出hello world!<script>alert()</script> ?></p> <p><?= HtmlPurifier::process($str); // 輸出hello world! ?></p> ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看