Ben Ben - 1 year ago 146
HTML Question

Using html-tags within HTMTL::link_to_route()

In Laravel, how can I use html-tags when linking to a route via


Example of what I have:

{{ HTML::link_to_route( "books_new", "New Book" ) }}

What I would like to do:

{{ HTML::link_to_route(
"<span class='icon-book'></span>New Book"
) }}

Answer Source

I know this is not the answer you want to hear - but you cannot pass html 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;; 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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download