19

I'm currently developing a little Sinatra Ruby app. For the presentation layer I'm using HAML, which works quite well.

However I have a FAQ page which has longer text paragraphs, which is not a problem. But in some passages I'd like to include links. With pure HTML this is very easy to achieve and is still readable.

<p>
I'm talking about <a href="http://ruby-lang.org">Ruby</a> in this text.
</p>

With HAML I'm stuck with this new paragraphs, which then looks like this:

%p
 I'm talking about 
 %a {:href => 'http://ruby-lang.org'}>Ruby 
 in this text.

I think this really breaks the workflow.

Is there a way in HAML to just insert a link without using a newline?

leifg
  • 8,668
  • 13
  • 53
  • 79

5 Answers5

57

It's easier than you might think. Just read this reference: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#whitespace_removal__and_

So usage is simple, just add less-sign "<" after your %p tag, e.g.:

%p<
 I'm talking about 
 %a {:href => 'http://ruby-lang.org'}>Ruby 
 in this text.

And it will generate exactly that you want to do.

Dmitry Polushkin
  • 3,283
  • 1
  • 38
  • 44
  • 4
    why isn't this marked as the correct answer? Thank you Dmitry! – a--m May 02 '12 at 14:29
  • 1
    @dome this removes the newlines from the resulting HTML, but they are still required in the markup, so this doesn't actually answer the question. – Thilo Aug 07 '14 at 06:57
  • 1
    This doesn't work because it removes the whitespaces around the link, as stated in matt's answer – paul Jan 21 '15 at 22:04
  • This does not work, it just removes the first new line. In other words, it would generate `

    I'm talking about\n

    – Filip Kis Apr 16 '16 at 10:44
  • @FilipKis if you mean newline before link tag, then you have to use the same pattern with `%a` as well. – Dmitry Polushkin Apr 16 '16 at 15:10
  • Not sure what you mean @DmitryPolushkin. Actually, I noticed I said wrongly, what you wrote would work, but it wouldn't have a space between the anchor: `I'm talking aboutin this text` and that was exactly the original problem. – Filip Kis Apr 16 '16 at 15:54
  • @FilipKis now I see your problem. I'm not using HAML anymore in production, so I cannot help you... but from the reference it seems not possible to preserve newline in the nested tags. I would suggest you to use slim: https://github.com/slim-template/slim, as it's faster and more native (if you are using ruby language). – Dmitry Polushkin Apr 16 '16 at 21:41
  • Updated link: http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace-removal--and- – Jared Oct 26 '17 at 19:23
18

Not familiar with Sinatra, but in Rails you can just do this:

%p
  I'm talking about #{ link_to 'Ruby', 'http://ruby-lang.org'} in this text.
Thilo
  • 17,565
  • 5
  • 68
  • 84
  • Plus 1! You can also make it a one liner if you want. – Michael Koper May 01 '11 at 21:07
  • Yes you could, but leifg explicitly mentions long text, so I assume this is just a fragment of a larger paragraph. Anyway - does this work in Sinatra, or is this a Rails specific syntax? I'm really not sure. – Thilo May 01 '11 at 22:38
  • `link_to` is [Rails specific](https://github.com/rails/rails/blob/fa77665a342b2486760c69007af4352cb40aae1e/actionpack/lib/action_view/helpers/url_helper.rb#L231), although it's pretty simple to create a similar [helper function in Sinatra](http://www.sinatrarb.com/intro.html#Helpers). – matt May 02 '11 at 23:06
  • [here](https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/url_helper.rb#L231) is the code used in rails for the link_to helper. You could probably pare this down to just (*args) unless you ever think you might want to use a block. I would probably create a helper in HAML in [this](https://github.com/nex3/haml/blob/master/lib/haml/helpers.rb) file for the link_to method. – Caley Woods May 04 '11 at 16:05
12

I’m assuming you”re asking about adding links without newlines in the source Haml. It's not really possible to do what you want here. From the haml FAQ:

Expressing the structure of a document and expressing inline formatting are two very different problems. Haml is mostly designed for structure, so the best way to deal with formatting is to leave it to other languages that are designed for it.

Haml uses whitespace and indentation to determine what to do, and without any newlines there isn't any indentation, and so haml can't determine what elements to add to the page.

If you just want blocks of static text, you could use the markdown filter like the FAQ suggests like this:

:markdown
  I'm talking about [Ruby](http://ruby-lang.org) in this text.

As other answers have pointed out, if you want to remove newlines from the generated HTML then you can look into using the whitespace removal operators, but note that they will remove all whitespace, so that for example

I'm talking about 
%a{:href => 'http://ruby-lang.org'}>Ruby 
in this text.

which uses > to remove outer whitespace will produce:

I'm talking about<a href='http://ruby-lang.org'>Ruby</a>in this text.

and this will appear as

I'm talking aboutRubyin this text.

i.e. without spaces around “Ruby”, which is probably not what you want.

davetapley
  • 17,000
  • 12
  • 60
  • 86
matt
  • 78,533
  • 8
  • 163
  • 197
  • This is incorrect - it is possible. Sadiq's answer using `link_to` works. – paul Jan 21 '15 at 22:05
  • @paul Sadiq’s answer contains multiple lines. The question is asking about avoiding newlines _in the source_, not in the rendered output. – matt Jan 21 '15 at 22:23
  • Oh, I see - that's confusing - thanks for clarifying. – paul Jan 21 '15 at 22:39
7

perfectly it is:

%p
  I'm talking about 
  = link_to 'Ruby', 'http://ruby-lang.org'
  in this text.
Sadık
  • 4,249
  • 7
  • 53
  • 89
7

You can also use inline HTML in HAML.

%p I'm talking about <a href="http://ruby-lang.org">Ruby</a> in this text.
postfuturist
  • 22,211
  • 11
  • 65
  • 85