当有数据时,显示数据,没内容则不显示。使用If条件,php直接写在html中的写法如下:
<?php if($model->is_top){ ?><span>置顶</span><?php } ?>
<!--简介有内容显示,没内容不显示-->
<?php if ($model->content): ?>
简介:<?= $model->content ?>
<?php endif; ?>
当有数据时,显示数据,没数据时,显示指定图片或者指定提示语
<?= $model->comment ? $model->comment : '暂时没有评语哦'?>
// 优秀作品当没图片时,显示一个灰色盒子start
$div = '';
if($model->imageModel){
$a = Html::a($img, $url);
}else {
$div = Html::tag('div', '', ['class' => 'without-img']);
}
$dt = Html::tag('dt', $a .$div);
<?= !empty($model->imageModel) ? Html::a(Html::img(Yii::getAlias($model->imageModel[0]->local_path), ['width' => 300]), $url) : $div = Html::tag('div', '', ['class' => 'without-img']); ?>