Eric the Red
Eric the Red

Reputation: 5454

How do I put a link in the middle of a paragraph with HAML?

How do I create this:

<p>
  I would like to make a <a href="foo.html">link</a> in my Rails app.
</p>

with HAML?

Upvotes: 18

Views: 20430

Answers (4)

Jeremy Weiskotten
Jeremy Weiskotten

Reputation: 978

I recommend reading Chris Eppstein's post "Haml Sucks for Content", and using something like Markdown or Textile to handle inline markup. I'm a big fan of Haml for document structure and a simple link in a paragraph is simple enough, but Haml starts to get out of control pretty fast.

Chris mentions that Haml is great for:

  1. making "the structure of the document obvious by forcing indentation", and
  2. Making it "easier to switch between styles and markup".

It is less great for when you have paragraphs of content you want to put in your document.

He suggests a few strategies to make it easier to embed text content in your Haml templates:

  1. Inline your HTML into your Haml document.
  2. Use filters, like :plain, :erb :markdown, etc. (Note :markdown and other filters may require some Haml extensions to be installed.
  3. Use a partial that uses another templating language.

Upvotes: 5

hellomello
hellomello

Reputation: 8597

If you know your routes, then you can do this:

%p
  I would like to make a #{link_to "link", foo_path} in my Rails app.

Pretty simple. Just wrap your Ruby syntax with #{}.

Upvotes: 6

Mike Woodhouse
Mike Woodhouse

Reputation: 52326

The "pure" HAML way:

%p
  I would like to make a 
  %a{:href => "foo.html"} link
  in my Rails app.

Using the Rails link_to helper:

%p
  I would like to make a 
  =link_to "link", "foo.html"
  in my Rails app.

Upvotes: 16

Daniel O&#39;Hara
Daniel O&#39;Hara

Reputation: 13438

1.

%p
  I would like to make a
  %a
    link
  in my Rails app.

2.

%p
  I would like to make a <a href="#">link</a> in my Rails app.

Upvotes: 29

Related Questions