文章出處

在views中建立一個common.php文件,然后把views中的index.php和about.php公共代碼放進去

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <?=$content;?>
 9 </body>
10 </html>

注意:這里的 $content 沒有在任何一個地方賦值

index.php中的內容為: hello index 

about.php中的內容為: hello about 

然后在controller中相應的代碼為:

設定公共文件: public $layout = 'common'; 

然后使用render函數輸出相應文件中的內容:

return $this->render('about');

則輸出: hello about  

 

 

2、在index.php中顯示about.php中的內容:

在index.php中,加上如下代碼:

<?php echo $this->render('about',array('v_hello_str'=>'hello'));?>

其中 array('v_hello_str'=>'hello') 是將該數組增加上about.php中

about.php中的內容為:

hello about
<?=$v_hello_str;?>

在controller中的代碼為

public $layout = 'common';
    public function actionIndex() {
        return $this->renderPartial('index');
    }

這時使用 renderPartial 和 render 效果是一樣的。

輸出為: hello index hello about hello 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()