1 回答

TA貢獻1995條經驗 獲得超2個贊
由于您可以通過視圖上下文訪問控制器實例$this
,我建議您這樣做:
創建局部視圖 php 文件,您可以在其中構建您的 mixin(js + php),它顯然將包含任何腳本類型,并在 PHP 之上具有某些條件。
在視圖上下文中使用
CBaseController#renderPartial
(實際上返回字符串而不是渲染,根據第三個參數return = true
)將混合視圖內容作為字符串獲取并將其作為第二個參數傳遞給Yii::app()->clientScript->registerScript
。
實現看起來像這樣:
// _partial_view.php
// here is your mixin between js + php
/* @var $this CController */
<?php
echo '<script>' .
Yii::app()->user->isGuest
? 'alert("you\'re guest")'
: 'alert("you\'re logged user")'
. '</script>';
然后返回到您注冊的 js 調用:
<?php Yii::app()->clientScript
->registerScript("form",
$this->renderPartial('_partial_view.php', [], true), // setting 3rd parameter to true is crucial in order to capture the string content instead of rendering it into the view
CClientScript::POS_END); ?>
- 1 回答
- 0 關注
- 113 瀏覽
添加回答
舉報