laravel 使用 HTML Helper 创建图像链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19007999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:38:30  来源:igfitidea点击:

Creating Image link with HTML Helper

imagelaravelanchorlaravel-4html-helper

提问by mXX

I'm trying to create an image link with the HTML helper of Laravel 4. But it seems that isn't really working. I have this line of code

我正在尝试使用 Laravel 4 的 HTML 帮助程序创建图像链接。但这似乎并没有真正起作用。我有这行代码

{{ HTML::link("#", HTML::image("img/logo.png", "Logo") ) }}

But that just outputs a strin like this:

但这只是输出一个像这样的字符串:

<img src="http://localhost/worker/public/img/logo" alt="Logo">

How come.??

怎么来的。??

回答by kJamesy

I think it's overkill for no reason. I would do:

我认为这是无缘无故的矫枉过正。我会做:

<a href="#"><img src={{asset('img/logo.png')}} alt="Logo"></a>

If I then need a dynamic link in place of the #, I would do:

如果我需要一个动态链接来代替#,我会这样做:

<a href="{{URL::to('/')}}"><img src={{asset('img/logo.png')}} alt="Logo"></a>

Try to use html as much as you can.

尽量使用 html。

回答by Antonio Carlos Ribeiro

You probably will have to:

您可能必须:

<a href="#">{{ HTML::image("img/logo.png", "Logo") }}</a>

Because, link() uses entities to escape the title:

因为,link() 使用实体来转义标题:

public function link($url, $title = null, $attributes = array(), $secure = null)
{
    $url = $this->url->to($url, array(), $secure);

    if (is_null($title) or $title === false) $title = $url;

    return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
}

Producing this source code:

生成此源代码:

"<a href="#">&lt;img src=&quot;http://localhost/img/logo.png&quot; alt=&quot;Logo&quot;&gt;</a>"

回答by Greg

You could also use html_entity_decode to get your code working

您还可以使用 html_entity_decode 让您的代码正常工作

{{ html_entity_decode( HTML::link("#", HTML::image("img/logo.png", "Logo") ) ) }}

回答by Shubham Bansal

HTML helper has been removed from Laravel 5

HTML 助手已从 Laravel 5 中删除

now you could simple use this

现在你可以简单地使用这个

<img src="{{ asset('logo.png') }}" alt="logo">

This asset by default point to public folder of your larval application

默认情况下,此资产指向幼虫应用程序的公共文件夹

回答by ErcanE

Additional to correct answer also you can add some attribute like heightand width

除了正确答案之外,您还可以添加一些属性,如高度宽度

<a href="#">{{ HTML::image("img/logo.png", "Logo",array('height'=>'100','width'=>'100')) }}</a>

回答by Anton

\Html::tag('a', \Html::image('image path')->toHtml(), ['href' => 'link']);