Ruby-on-rails Haml:控制文本周围的空白
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1311428/
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
Haml: Control whitespace around text
提问by Matchu
In my Rails template, I'd like to accomplish final HTML to this effect using HAML:
在我的 Rails 模板中,我想使用 HAML 完成最终的 HTML 以达到这种效果:
I will first <a href="http://example.com">link somewhere</a>, then render this half of the sentence if a condition is met
The template that comes close:
接近的模板:
I will first
= link_to 'link somewhere', 'http://example.com'
- if @condition
, then render this half of the sentence if a condition is met
You may, however, note that this produces a space between the link and the comma. Is there any practical way to avoid this whitespace? I know there's syntax to remove whitespace around tags, but can this same syntax be applied to just text? I really don't like the solution of extra markup to accomplish this.
但是,您可能会注意到这会在链接和逗号之间产生一个空格。有什么实用的方法可以避免这个空白吗?我知道有删除标签周围空格的语法,但是可以将相同的语法应用于文本吗?我真的不喜欢额外标记的解决方案来实现这一点。
回答by Ryan Crispin Heneise
A better way to do this has been introduced via Haml's helpers:
通过 Haml 的助手引入了一种更好的方法:
surround
环绕
= surround '(', ')' do
%a{:href => "food"} chicken
产生:
(<a href='food'>chicken</a>)
succeed:
成功:
click
= succeed '.' do
%a{:href=>"thing"} here
产生:
click
<a href='thing'>here</a>.
precede:
先于:
= precede '*' do
%span.small Not really
产生:
*<span class='small'>Not really</span>
To answer the original question:
要回答原始问题:
I will first
= succeed ',' do
= link_to 'link somewhere', 'http://example.com'
- if @condition
then render this half of the sentence if a condition is met
产生:
I will first
<a href="http://example.com">link somewhere</a>,
then render this half of the sentence if a condition is met
回答by Ryan Crispin Heneise
You can also do this using Haml's "trim whitespace" modifier. Inserting >after a Haml declaration will prevent whitespace from being added around it:
您也可以使用 Haml 的“修剪空白”修饰符来做到这一点。>在 Haml 声明之后插入将防止在其周围添加空格:
I will first
%a{:href => 'http://example.com'}> link somewhere
- if @condition
, then render this half of the sentence if a condition is met
produces:
产生:
I will first<a href='http://example.com'>link somewhere</a>, then render this half of the sentence if a condition is met
However, as you can see, the >modifier also strips the whitespace in front of the link, removing the desired space between the words and the link. I haven't figured a pretty way around this yet, except to add to the end of "I will first", like so:
但是,如您所见,>修饰符还会去除链接前的空白,删除单词和链接之间所需的空格。我还没有想出一个很好的方法来解决这个问题,除了添加 到“我会先”的末尾,像这样:
I will first
%a{:href => 'http://example.com'}> link somewhere
- if @condition
, then render this half of the sentence if a condition is met
Which finally produces the desired output without lots of hard-to-read interpolation:
最终产生所需的输出而没有大量难以阅读的插值:
I will first <span><a href="http://example.com">link somewhere</a></span>, then render this half of the sentence if a condition is met
回答by Matchu
Alright, here's the solution I'm settling on:
好的,这是我正在解决的解决方案:
Helper
帮手
def one_line(&block)
haml_concat capture_haml(&block).gsub("\n", '').gsub('\n', "\n")
end
View
看法
I will first
- one_line do
= link_to 'link somewhere', 'http://example.com'
- if @condition
, then render this half of the sentence
\n
if a condition is met
That way, whitespace is excluded by default, but I can still explicitly include it with a "\n" line. (It needs the double-backslash because otherwise HAML interprets it as an actual newline.) Let me know if there's a better option out there!
这样,默认情况下会排除空格,但我仍然可以使用 "\n" 行明确包含它。(它需要双反斜杠,否则 HAML 会将其解释为实际的换行符。)如果有更好的选择,请告诉我!
回答by Yo Ludke
You can use the 'aligator syntax' of HAML
您可以使用 HAML 的“aligator 语法”
Whitespace Removal: > and <
and < give you more control over the whitespace near a tag. > will remove all whitespace surrounding a tag, while < will remove all whitespace immediately within a tag. You can think of them as alligators eating the whitespace: > faces out of the tag and eats the whitespace on the outside, and < faces into the tag and eats the whitespace on the inside. They're placed at the end of a tag definition, after class, id, and attribute declarations but before / or =.
空格去除:> 和 <
和 < 让您更好地控制标签附近的空白。> 将删除标签周围的所有空白,而 < 将立即删除标签内的所有空白。你可以把它们想象成吃空白的鳄鱼: > 面向标签,吃掉外面的空白,< 面向标签,吃掉里面的空白。它们位于标签定义的末尾,在类、id 和属性声明之后但在 / 或 = 之前。
http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace_removal__and_
http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace_removal__and_
回答by Chuck
Once approach I've taken to this sort of thing is to use string interpolation:
我对这种事情采取的方法是使用字符串插值:
I will first #{link_to 'Link somewhere'}#{', then render this half of the sentence if a condition is met' if condition}
I don't like the look of the literal string in the interpolation, but I've used it with previously declared strings or dynamically generated strings before.
我不喜欢插值中文字字符串的外观,但我之前已经将它与先前声明的字符串或动态生成的字符串一起使用。
回答by thethinman
You can do this to keep the leading space:
您可以这样做以保持领先的空间:
%a{:href => 'http://example.com'}>= ' link somewhere'
The space is in the quotes.
空格在引号中。
回答by ojak
Although not well documented, this is cleanly achieved using HAML whitespace preservation (>) combined with an ASCII space (& #32;), and not with helpers:
尽管没有很好的文档记录,但使用 HAML 空白保留 (>) 与 ASCII 空间 (& #32;) 相结合,而不是使用助手,可以清楚地实现这一点:
%a{:href=>'/home'}> Home link
, 
%a{:href=>'/page'} Next link
This will produce what you want:
这将产生你想要的:
<a href='/home'>Anchor text</a>, 
<a href='/page'>More text</a>
But I agree, HAML needs to come up with a better way of doing this, as it does add unnecessary ASCII characters to the page (but it's still more efficient than using helpers).
但我同意,HAML 需要想出一种更好的方法来做到这一点,因为它确实向页面添加了不必要的 ASCII 字符(但它仍然比使用助手更有效)。
回答by biscuits
I came across a similar problem and found this so I thought I would post another solution which doesn't require a helper method. Use Ruby interpolation #{} to wrap the link and if statements:
我遇到了类似的问题并发现了这一点,所以我想我会发布另一个不需要辅助方法的解决方案。使用 Ruby 插值 #{} 来包装链接和 if 语句:
I will first
#{link_to 'link somewhere', 'http://example.com'}#{if true : ", then render this half of the sentence if a condition is met" end}
This works in 3.0.18, it may also work in earlier releases.
这适用于 3.0.18,它也可能适用于早期版本。
回答by Andrew Vit
There's the angle bracket "whitespace munching" syntax, otherwise write a helper method for it.
有尖括号“空白咀嚼”语法,否则为它编写一个辅助方法。
回答by colllin
Yet another option that I've used in the past:
我过去使用过的另一个选项:
- if @condition
%span> , then some more text after the link.

