flavaflo
flavaflo

Reputation: 918

put haml tags inside link_to helper

is it possible to add html-content inside a link_to helper in HAML?

i tried this, but all i get is a syntax error:

= link_to "Other page", "path/to/page.html"
    %span.icon Arrow

expected output:

<a href="path/to/page.html">Other Page<span class="icon">Arrow</span></a>

Upvotes: 49

Views: 25104

Answers (3)

Paul
Paul

Reputation: 2031

If anyone is still using Rails 2.x on a project, it looks like the accepted answer returns the block, thus duplicating the link in the markup. Very simple change: use - instead of =

- link_to "path/to/page.html" do
  Other page
  %span.icon Arrow

Upvotes: 12

Nikhil Nanjappa
Nikhil Nanjappa

Reputation: 6652

The simplest way to do it is by using html_safe or raw functions

= link_to 'Other Page<span class="icon"></span>'.html_safe, "path/to/page.html"

or using raw function (recommended)

= link_to raw('Other Page<span class="icon"></span>'), "path/to/page.html"

Simple as it can get !!

Don’t use html_safe method unless you’re sure your string isn’t nil. Instead use the raw() method, which wont raise an exception on nil.

Upvotes: 5

Michał Szajbe
Michał Szajbe

Reputation: 9002

You should use block

= link_to "path/to/page.html" do
  Other page
  %span.icon Arrow

Upvotes: 111

Related Questions