本文介绍了Yii2 >如何将 _form 视图的 html 输出存储到字符串变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将使用 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
<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']) ?>
<?php ActiveForm::end();?>
如果有人想出一个很棒的解决方案..
解决方案
我已经在一个简单的 ControllerAction 中尝试过这种方式并且工作正常......在 var_dump($test) 那里得到了切面的结果
公共函数actionView($id){$test = $this->renderPartial('_form', ['模型' =>$this->findModel($id),]);var_dump($test);}
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..
解决方案
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);
}
这篇关于Yii2 >如何将 _form 视图的 html 输出存储到字符串变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!