Yii2 > How to store html output of a _form view into a string variable

不打扰是莪最后的温柔 提交于 2019-12-10 15:56:37

问题


I want to store the rendered html output of a form view which uses ActiveForm and Html Helper into a variable within my Controller.

I've tried storing result of a renderPartial directly to a variable, which did not work:

$htmlform = Yii::$app->controller->renderPartial('_form', ['model' => $model]);

I've also tried using output buffering to echo the output into a variable, but I could not store the output:

ob_start();
echo Yii::$app->controller->renderPartial('_form', ['model' => $model]);
$htmlform = ob_get_contents();
ob_end_clean();

View File: _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(); ?>

If anyone has an idea on a solution that would be great..


回答1:


I have tried this way in a simple ControllerAction and work right ...in var_dump($test) there the aspected result

public function actionView($id)
{
    $test =  $this->renderPartial('_form', [
        'model' => $this->findModel($id),
    ]);
    var_dump($test);

}


来源:https://stackoverflow.com/questions/35910411/yii2-how-to-store-html-output-of-a-form-view-into-a-string-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!