Search code examples
phpyiiyii2yii2-advanced-appyii2-basic-app

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


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..


Solution

  • 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);
    
    }