Html HAML 在没有换行符的情况下使用文本中的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5850952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:20:39  来源:igfitidea点击:

HAML using links in text without newline

htmlsinatrahaml

提问by leifg

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

我目前正在开发一个小的 Sinatra Ruby 应用程序。对于表示层,我使用了 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.

但是,我有一个 FAQ 页面,其中包含更长的文本段落,这不是问题。但在某些段落中,我想包含链接。使用纯 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 中有没有办法只插入一个链接而不使用换行符?

采纳答案by matt

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:

我假设您正在询问在源 Haml 中添加不带换行符的链接。在这里做你想做的事是不可能的。来自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 filterlike the FAQ suggests like this:

如果你只想要静态文本块,你可以像 FAQ 建议的那样使用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 HTMLthen you can look into using the whitespace removal operators, but note that they will remove allwhitespace, 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 aboutRubyin this text.

我在本文中谈论Ruby

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

即“Ruby”周围没有空格,这可能不是您想要的。

回答by Dmitry Polushkin

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, e.g.:

所以用法很简单,只需在你的 %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.

它会生成你想要做的。

回答by Thilo

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.

回答by postfuturist

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.

回答by Sadik

perfectly it is:

完美的是:

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