简体   繁体   English

HAML 在没有换行符的文本中使用链接

[英]HAML using links in text without newline

I'm currently developing a little Sinatra Ruby app.我目前正在开发一个小 Sinatra Ruby 应用程序。 For the presentation layer I'm using HAML, which works quite well.对于表示层,我使用的是 HAML,效果很好。

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.使用纯 HTML 这很容易实现并且仍然可读。

<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:使用 HAML,我坚持使用这个新段落,然后看起来像这样:

%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? HAML 中有没有一种方法可以在不使用换行符的情况下插入链接?

It's easier than you might think.这比你想象的要容易。 Just read this reference: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#whitespace_removal__and_只需阅读此参考: 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 标记后添加减号“<”,例如:

%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:不熟悉 Sinatra,但在 Rails 中你可以这样做:

%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 .我假设您正在询问在源 Haml中添加不带换行符的链接。 It's not really possible to do what you want here.在这里做你想做的事情是不可能的。 From the haml FAQ :haml常见问题解答

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 主要是为结构设计的,因此处理格式的最佳方法是将其留给为其设计的其他语言。

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. Haml 使用空格和缩进来确定要做什么,并且没有任何换行符就没有任何缩进,因此 haml 无法确定要添加到页面中的元素。

If you just want blocks of static text, you could use the markdown filter like the FAQ suggests like this:如果您只想要 static 文本块,则可以使用markdown 过滤器,如常见问题解答中建议的那样:

: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正如其他答案所指出的那样,如果您想从生成的 HTML中删除换行符,那么您可以考虑使用 空格删除运算符,但请注意它们将删除所有空格,例如

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.我说的是本文中的Ruby

ie without spaces around “Ruby”, which is probably not what you want.即“Ruby”周围没有空格,这可能不是您想要的。

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.您还可以在 HAML 中使用内联 HTML。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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