문제

I'm working on laravel php framework , I've a simple question , While using blade template engine , I normally use html helper functions to render forms , links or whatever so .

Now when i try to put a link to specific route , i usually to like that :

{{ HTML::link_to_route('author','TheAuthor')) }}

First parameter takes the route , and the second one takes the string that will appear , To make it simpler that code will produce :

<a href="ROUTE URL">TheAuthor</a>

Now I want to replace TheAuthor with an image , What can i do ?

도움이 되었습니까?

해결책 2

I know this is not the answer you want to hear - but you cannot pass any image via link_to_route.

The problem is the output from the HTML class is escaped automatically. So if you try to pass this:

{{ HTML::link_to_route('author','<img src="'.URL::base().'assets/images/image.jpg" alt="icon" />')) }}

it comes out like this:

&lt;img src=&quot;http://laravel3.dev/assets/images/image.jpg&quot; alt=&quot;icon&quot; /&gt;

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):

function link_to_route_image($route, $image)
{
   $m = '<a href="'.URL::to_route($route).'">'
      . '<img>'.$image.'</img>'
      . '</a>';
   return $m;
}

다른 팁

Or you can use {{route}}

Ex:

<a href="{{route($route,$params)}}"><img src="'.$image.'" /></a>

It may become tedious if you are doing this all over the place, but in isolated spots the best way to handle this would actually be to wrap your entire link_to_* method call in HTML::decode.

{{ HTML::decode(HTML::link_to_route('author','<img src="{URL::base()}assets/images/image.jpg" alt="icon" />')) }}

There's nothing you can do from the inside of the function (such as wrapping the "title" portion in decode) because the cleaning is happening within the function call, but if you decode the entire link it will render the html properly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top