yii2-advanced-app - 如何使用带有yii2语法的html代码增加图片?
问题描述
嗨伙计们,以下代码将通过单击它来增加图片:
<a class="example-image-link" href="http://lokeshdhakar.com/projects/lightbox2/images/image-1.jpg"><img src="http://lokeshdhakar.com/projects/lightbox2/images/thumb-1.jpg" alt="image-1" /></a>
)
在我的 yii2 应用程序中,我在匿名函数中有以下有效图片 URL:
$url = '@web/img/' . $bild->dateiname;
如何在 yii2 语法中转换上层代码。我尝试这样,但它不起作用:
return Html::a(
Html::img($url, ['alt' => 'PicNotFound', 'class' => 'img-circle', 'style' => 'width:225px;height:225px']
), [Html::img($url, ['alt' => 'PicNotFound']
)], ['title' => 'Immobiliendaten abrufen', 'data' => ['pjax' => '0'], 'class' => 'example-image-link']
);
Firedebug 将显示以下 html 代码。链接中的url是错误的!
a class="example-image-link" href="/yii2_ErkanImmo/frontend/web/index.php/" title="Immobiliendaten abrufen"><img class="img-circle" src="/yii2_ErkanImmo/frontend/web/img/villa1.jpg" alt="PicNotFound" style="width:125px;height:125px;">event
<img class="img-circle" src="/yii2_ErkanImmo/frontend/web/img/villa1.jpg" alt="PicNotFound" style="width:125px;height:125px;">
</a>
改写问题
在使用如下代码单击图片后,我将获得如附件所示的输出:
return Html::a(Html::img($url, ['alt' => 'image-1']), $url, ['class' => 'example-image-link']);[![enter image description here][1]][1]
这是视图文件的完整代码(用于 GRU)
[
'attribute' => $dummy,
'label' => Yii::t('app', ''),
'format' => 'html', // sorgt dafür,dass das HTML im return gerendert wird
'vAlign' => 'middle',
'value' => function($model) {
$bmp = '/bmp/';
$tif = '/tif/';
$png = '/png/';
$psd = '/psd/';
$pcx = '/pcx/';
$gif = '/gif/';
$jpeg = '/jpeg/';
$jpg = '/jpg/';
$ico = '/ico/';
try {
$bilder = \frontend\models\Dateianhang::GetBild($model);
foreach ($bilder as $bild) {
if (preg_match($bmp, $bild->dateiname) || preg_match($tif, $bild->dateiname) || preg_match($png, $bild->dateiname) || preg_match($psd, $bild->dateiname) || preg_match($pcx, $bild->dateiname) || preg_match($gif, $bild->dateiname) || preg_match($jpeg, $bild->dateiname) || preg_match($jpg, $bild->dateiname) || preg_match($ico, $bild->dateiname)) {
$url = '@web/img/' . $bild->dateiname;
}
}
return Html::a(Html::img($url, ['alt' => 'image-1', 'class' => 'img-circle', 'style' => 'width:225px;height:225px']), $url, ['class' => 'example-image-link', 'title' => 'Touch picture to increase', 'target' => '_blank']);
//return Html::img($url, ['alt' => 'Bewerberbild nicht vorhanden', 'class' => 'img-circle', 'style' => 'width:225px;height:225px']);
} catch (Exception $e) {
return;
}
}
],
既不会增加图片,也不会增加 target=>_blank 的工作:=(
解决方案
尝试以下代码 -
<?php
$url = 'http://lokeshdhakar.com/projects/lightbox2/images/thumb-1.jpg';
//in your case
//$url = '@web/img/'.$bild->dateiname;
echo Html::a(
Html::img($url, ['alt'=>'image-1']),
$url, ['class'=>'example-image-link']);
?>
如果要重定向到控制器中的某些操作,请将$url
最后一行替换为['/controller/action']
推荐阅读
- kubernetes - 在不同主机上使用复制容器安排作业
- javascript - d3 为选择获取数据绑定有效,但是当我迭代选择时,它不起作用
- machine-learning - CatBoost 机器学习超参数:为什么不总是使用 `thread_count = -1`?
- python-3.x - 使用 PRAW 时出现 401 状态错误(reddit)
- python - 用于查找最大值的列的动态连接
- git - 前面的正则表达式 git secret 无效
- swift - 如何在 Firebase 动态链接 iOS Swift 中添加 OFL 链接?
- c++ - 如何将多个数字优化为一个短字符串,然后再转换回来?
- python - Pipenv 没有创建 Pipfile 也没有在正确的位置创建环境
- java - 无法在 Android 中发送带有 PDF 文件作为附件的电子邮件