laravel 在 HTMTL::link_to_route() 中使用 html-tags
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14834471/
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
Using html-tags within HTMTL::link_to_route()
提问by Ben
In Laravel, how can I use html-tags when linking to a route via HTML::link_to_route()
?
在 Laravel 中,通过 链接到路由时如何使用 html-tags HTML::link_to_route()
?
Example of what I have:
我所拥有的示例:
<li>
{{ HTML::link_to_route( "books_new", "New Book" ) }}
</li>
What I would like to do:
我想做什么:
<li>
{{ HTML::link_to_route(
"books_new",
"<span class='icon-book'></span>New Book"
) }}
</li>
回答by Laurence
I know this is not the answer you want to hear - but you cannot pass html via link_to_route.
我知道这不是您想听到的答案 - 但您无法通过 link_to_route 传递 html。
The problem is the output from the HTML class is escaped automatically. So if you try to pass this:
问题是 HTML 类的输出会自动转义。所以如果你试图通过这个:
{{ HTML::link_to_route('author','<img src="'.URL::base().'assets/images/image.jpg" alt="icon" />')) }}
it comes out like this:
它是这样出来的:
<img src="http://laravel3.dev/assets/images/image.jpg" alt="icon" />
which will just be text on the screen - no image. Instead you need to use URI::to_route('author')
and generate the link yourself. So make a helper a like this (not tested):
这将只是屏幕上的文本 - 没有图像。相反,您需要自己使用URI::to_route('author')
和生成链接。所以做一个这样的助手(未测试):
function link_to_route_image($route, $image)
{
$m = '<a href="'.URL::to_route($route).'">'
. '<img>'.$image.'</img>'
. '</a>';
return $m;
}
回答by Ktee8
How about something like this?
这样的事情怎么样?
<li>
<a href="{{ URL::to_route('books_new') }}"><span class='icon-book'></span>New Book</a>
</li>
If you're using "Font Awesome", just adding the class to anchor tag as someone mentioned would be fine for most cases because "Icon classes are echoed via CSS :before". You might need a bit of adjustment in CSS; but it might be better in terms of semantic mark-up.
如果您使用的是“Font Awesome”,那么在大多数情况下,只需将类添加到锚标记中就可以了,因为“图标类通过 CSS :before 回显”。您可能需要对 CSS 进行一些调整;但在语义标记方面可能会更好。
回答by user2560156
<a href="{{ URL::route('empdelete', array('id' => $employee->id)) }}">
<img src="{{ asset('images/tick-red.jpg') }}" alt="DRC" id="DRCS-logo" /></a>
回答by Nima Ghaedsharafi
You can not have HTML markup with HTML::....
(class) , in the documentation they say that anything that is passed as a parameter to the class is escaped with an HTML entity function to make front-end safer!
您不能使用HTML::....
(class)进行 HTML 标记,在文档中他们说任何作为参数传递给类的内容都使用 HTML 实体函数进行转义,以使前端更安全!
回答by iwan.webdeveloper
You can include font awesome or icon into Laravel Blade Template using this code, i already use and work perfect.
您可以使用此代码将字体真棒或图标包含到 Laravel Blade 模板中,我已经使用并完美运行。
<a href="{{ route('member.edit',$data->id) }}"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit</a>
回答by Sandy
If you're using "Font Awesome", just adding the class to anchor tag as someone mentioned would be fine for most cases because "Icon classes are echoed via CSS :before".
如果您使用的是“Font Awesome”,那么在大多数情况下,只需将类添加到锚标记中就可以了,因为“图标类通过 CSS :before 回显”。
So this is working for me:
所以这对我有用:
<li>
{{ HTML::link_to_route( "books_new", "New Book", null, ['class' => 'fa fa-edit'] ) }}
</li>
回答by Mike Rockétt
So far as I know, Laravel doesn't allow you to do that. To me, it seems out of standards.
据我所知,Laravel 不允许你这样做。在我看来,这似乎不合标准。
Rather, apply a class called icon-book
to your anchor tag, and then use the class to put the icon inside your anchor as a 'background-image`.
相反,将一个称为icon-book
锚标记的类应用到您的锚点标签上,然后使用该类将图标作为“背景图像”放置在您的锚点内。
HTML::link_to_route('books_new', 'New Book', array('class' => 'icon-book'))
Alternatively:
或者:
- Insert the
span
tag inside theli
tag - Assign the
icon-book
class to theli
tag
- 在
span
标签内插入li
标签 - 将
icon-book
类分配给li
标签