Yii2 > 如何将 _form 视图的 html 输出存储到字符串变量中
Yii2 > How to store html output of a _form view into a string variable
我想将使用 ActiveForm 和 Html Helper 的表单视图呈现的 html 输出存储到我的控制器中的一个变量中。
我试过将 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);
}
我想将使用 ActiveForm 和 Html Helper 的表单视图呈现的 html 输出存储到我的控制器中的一个变量中。
我试过将 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);
}