gpt4 book ai didi

php - Yii2 > 如何将 _form View 的 html 输出存储到字符串变量中

转载 作者:可可西里 更新时间:2023-10-31 23:28:50 24 4
gpt4 key购买 nike

我想将使用 ActiveForm 和 Html Helper 的表单 View 呈现的 html 输出存储到我的 Controller 中的一个变量中。

我试过将 renderPartial 的结果直接存储到一个变量,但没有成功:

$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);

}

关于php - Yii2 > 如何将 _form View 的 html 输出存储到字符串变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35910411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com