Php Yii2>;如何将表单视图的html输出存储到字符串变量中
我想将使用ActiveForm和html Helper的表单视图的呈现html输出存储到控制器内的变量中 我曾尝试将renderPartial的结果直接存储到变量中,但没有成功:Php Yii2>;如何将表单视图的html输出存储到字符串变量中,php,yii,yii2,yii2-advanced-app,yii2-basic-app,Php,Yii,Yii2,Yii2 Advanced App,Yii2 Basic App,我想将使用ActiveForm和html Helper的表单视图的呈现html输出存储到控制器内的变量中 我曾尝试将renderPartial的结果直接存储到变量中,但没有成功: $htmlform = Yii::$app->controller->renderPartial('_form', ['model' => $model]); 我还尝试使用输出缓冲将输出回显到变量中,但无法存储输出: ob_start(); echo Yii::$app->controller
$htmlform = Yii::$app->controller->renderPartial('_form', ['model' => $model]);
我还尝试使用输出缓冲将输出回显到变量中,但无法存储输出:
ob_start();
echo Yii::$app->controller->renderPartial('_form', ['model' => $model]);
$htmlform = ob_get_contents();
ob_end_clean();
查看文件:_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model frontend\models\Epic */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="epic-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'closed')->textInput() ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'organizationid')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
如果有人对解决方案有什么想法,那就太好了。我已经在一个简单的ControllerAction中尝试过这种方法,并且工作正常……在var_dump($test)中,会得到预期的结果
public function actionView($id)
{
$test = $this->renderPartial('_form', [
'model' => $this->findModel($id),
]);
var_dump($test);
}