我在yii 2的视图文件中制作if else语句时遇到问题,问题是使用了
<p>
<?php if(Yii::$app->user->can('create-car')){ ?>
<?= Html::a('Tambah Mobil', ['create'], ['class' => 'btn btn-success']) ?>
<?php}?>
</p>
当我运行该代码时,它会显示文件结束错误,实际上我的代码是什么问题?
答案 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 } ?>