Yii2渲染视图将HTML代码显示为文本

时间:2018-07-12 06:24:11

标签: yii2 yii2-advanced-app

我在Yii2中渲染一个视图时遇到了问题,这困扰了我几天了,渲染一直很好,直到有一天早上它决定不工作。该视图显示原始HTML代码而不是用户界面。

这是我的控制器的代码,用于渲染视图

if ($fails == TRUE)
{
    Yii::$app->session->setFlash('error', 'The following records failed validation, kindly update and submit');
    return $this->render('failedvalidation',['fails' => $fails,]);
} else {
    Yii::$app->session->setFlash('success', 'File uploaded successfully,waiting approval');
    return $this->redirect(Yii::$app->request->referrer);
}

下面是我的查看文件(failedvalidation.php)的代码

<?php
use yii\helpers\Html;
use yii\bootstrap\Modal;
use yii\helpers\Url;
?>
<div class="bulkpayments-view">
<div class="box box-default">
    <div class="table-responsive">
        <table class="display nowrap" id="example1">
            <thead>
            <tr>
                <th>Ref No</th>
                <th>Full Names</th>
                <th>Id No</th>
                <th>Credit Account</th>
                <th>Amount</th>
                <th>Narration</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach($fails as $data) {?>
                <tr class="gradeX">
                    <td><?= $data['BENEFICIARYREFNO'];?></td>
                    <td><?= $data['BENEFICIARYFULLNAMES'];?></td>
                    <td><?= $data['IDNO'];?></td>
                    <td><?= $data['TOACCOUNT'];?></td>
                    <td><?= $data['AMOUNT'];?></td>
                    <td><?= $data['NARRATION'];?></td>
                    <td><?= Html::button('<i class="fa fa-pencil"></i> 
    Update', ['value' => Url::to(['update-failed-validation', 'id' => 
    $data['ID'],'transid' => $data['TRANSACTIONID']]), 'class' => 'button btn 
     btn-primary btn-xs']) ?></td>

                </tr>
            <?php }?>
            </tbody>
        </table>
    </div>
</div>
</div>
<?php
Modal::begin([
'header' => '<h4>Update Record</h4>',
'id' => 'modal',
'size' => 'modal-lg',
'clientOptions' => ['backdrop' => 'static', 'keyboard' => false],
]);
  echo '<div id="content"></div>';
 Modal::end();
 ?>

<?php
$js = <<<JS
$('.button').click(function() {
if($('#modal').data('shown.bs.modal')) {
    $('#modal').find('#content')
        .html('<i class="fa fa-spinner fa-pulse fa-2x"></i>')
        .load($(this).attr('value'));
} 
else {
    $('#modal').modal('show')
        .find('#content')
        .html('<i class="fa fa-spinner fa-pulse fa-2x"></i>')
        .load($(this).attr('value'));
}
});
JS;
$this->registerJs($js);
?>
<?php
$script = <<< JS
$(function () { 
    $('#example1').DataTable( {
      dom: 'Bfrtip',
        buttons: [
        {
            //extend: 'excel',
            exportOptions: {
                orthogonal: 'sort'
            }
        }        
        ],
        columnDefs: [{
           targets:[0,1,2],
           render: function(data, type, row, meta){
              if(type === 'sort'){
                 //data = ' ' + data ;
                  return "\u200C" + data ; 
              }

              return data ;   

           }
        }]

} );
});
JS;
$this->registerJs($script);
?>
<style>
.box{
    padding:30px;
}
</style>

我在浏览器上的视图及其显示的原始HTML的屏幕截图:

View raw html display

1 个答案:

答案 0 :(得分:0)

嗨,我遇到了类似的情况,挣扎了几天。最终我发现在我的一个职能中有这样的陈述   \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; 所以我禁用了此声明。它有效,但是我需要找到更好的解决方案。希望有帮助