Ben Ben - 6 months ago 39
HTML Question

Using html-tags within HTMTL::link_to_route()

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

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>

Answer

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;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;
}