在主视图中,而不是 include do
<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>
更新:
protected/views/controller/page1.php和protected/views/controller/page2.php内容随心所欲
受保护/视图/布局/custom.php:
<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
<div id="menu">
<?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
<?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
</div>
<div id="content">
<?php echo $content; ?>
</div>
</div>
<?php $this->endContent(); ?>
受保护/控制器/ControllerController.php:
class ControllerController extends Controller {
/**
* @var string the default layout for the views.
*/
public $layout = '//layouts/custom';
public function actionPage1() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page1');
else
$this->render('page1');
}
public function actionPage2() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page2');
else
$this->render('page2');
}
}
更新2:
如果您也需要更改地址栏中的链接,那么您唯一的选择是使用常规链接而不是 ajax<?php echo CHtml::link('Page1', array('controller/page1')); ?>
使用 ajax 的首选方法是使用您提到的哈希。