Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HAML using links in text without newline

Tags:

html

sinatra

haml

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?

like image 293
leifg Avatar asked May 01 '11 19:05

leifg


4 Answers

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.

like image 122
Dmitry Polushkin Avatar answered Oct 23 '22 11:10

Dmitry Polushkin


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.
like image 36
Thilo Avatar answered Oct 23 '22 11:10

Thilo


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.

like image 12
matt Avatar answered Oct 23 '22 11:10

matt


You can also use inline HTML in HAML.

%p I'm talking about <a href="http://ruby-lang.org">Ruby</a> in this text.
like image 7
postfuturist Avatar answered Oct 23 '22 12:10

postfuturist