在视图yii2中创建if else语句

时间:2015-05-15 16:52:38

标签: php view tags yii2

我在yii 2的视图文件中制作if else语句时遇到问题,问题是使用了

<p>
   <?php if(Yii::$app->user->can('create-car')){ ?>
        <?= Html::a('Tambah Mobil', ['create'], ['class' => 'btn btn-success']) ?>
   <?php}?>
</p>

当我运行该代码时,它会显示文件结束错误,实际上我的代码是什么问题?

4 个答案:

答案 0 :(得分:3)

<p>
    <?php 
      if(Yii::$app->user->can('create-car')){ 
         echo  Html::a('Tambah Mobil', ['create'], ['class' => 'btn btn-success']);
      } 
    ?>
</p>

答案 1 :(得分:2)

你应该能够像这样做

<?php if (Yii::$app->user->can('create-car')): ?>
                        <?= Html::a('Tambah Mobil', ['create'], ['class' => 'btn btn-success']) ?>
<?php else: ?>
                        <li>Your HTML to be rendered when condition is false</li>
<?php endif; ?>

答案 2 :(得分:-1)

你可以试试这个:

<p>
   <?php if(Yii::$app->user->can('create-car')): ?>
        <?= Html::a('Tambah Mobil', ['create'], ['class' => 'btn btn-success']) ?>
   <?php endif; ?>
</p>

答案 3 :(得分:-1)

试试这个,我希望这会对你有所帮助

<?php
        if(Yii::$app->user->can('admin'))
            { ?>
            <?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
            <?= Html::a('Delete', ['delete', 'id' => $model->id], [
                'class' => 'btn btn-danger',
                'data' => [
                    'confirm' => 'Are you sure you want to delete this item?',
                    'method' => 'post',
                ],
            ]) ?>
        <?php } ?>