简体   繁体   中英

HAML using links in text without newline

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?

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, eg:

%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.

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.

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 about Ruby in this text.

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

perfectly it is:

%p
  I'm talking about 
  = link_to 'Ruby', 'http://ruby-lang.org'
  in this text.

You can also use inline HTML in HAML.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM