在我轨道的模板,我想完成最后HTML到这种效应使用HAML:

I will first <a href="http://example.com">link somewhere</a>, then render this half of the sentence if a condition is met

该模板,来关闭:

I will first
= link_to 'link somewhere', 'http://example.com'
- if @condition
  , then render this half of the sentence if a condition is met

你可以,但是,请注意,这将产生一个空间之间的链接,并逗号。是否有任何实际的方式来避免这一空白?我知道那里的语法删除的空白周围的标签,但是可以这相同的语法可以应用于仅仅是文字?我真的不喜欢的解决方案的额外的标记,以实现这一目标。

有帮助吗?

解决方案

一个更好的方式来做到这一点已经引入了经Haml的助手:

环绕

= surround '(', ')' do
  %a{:href => "food"} chicken
生产:
(<a href='food'>chicken</a>)

成功:

click
= succeed '.' do
  %a{:href=>"thing"} here
生产:
click
<a href='thing'>here</a>.

之前:

= precede '*' do
  %span.small Not really
生产:
*<span class='small'>Not really</span>

回答原来的问题:

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

其他提示

您也可以做到这一点使用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

产生

I will first<a href='http://example.com'>link somewhere</a>, then render this half of the sentence if a condition is met

然而,如可以看到的,>改性剂也条在链接前的空格,除去单词和连杆之间的期望空间。我还没有想出解决这个漂亮的路要走,除了&nbsp;增加的“我会先”结尾,像这样:

I will first&nbsp;
%a{:href => 'http://example.com'}> link somewhere
- if @condition
  , then render this half of the sentence if a condition is met

:哪个最后产生所需的输出,而不大量难以读插值的

I will first&nbsp;<span><a href="http://example.com">link somewhere</a></span>, then render this half of the sentence if a condition is met

好吧,这里是我解决的方案:

辅助

def one_line(&block)
  haml_concat capture_haml(&block).gsub("\n", '').gsub('\\n', "\n")
end

视图

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

这样,空格被默认排除,但我仍然可以明确地用“\ n”个线路包括它。 (它需要双反斜线,否则HAML其解释为实际的换行符。)让我知道如果有一个更好的选择在那里!

可以使用HAML的 '鳄鱼沉重语法'

  

空白移除:>和<

     

和<给你在附近的标签空白更多的控制。 >将删除包围标记的所有的空白,而<将立即删除一个标记内的所有空格。你可以认为他们是鳄鱼吃空白:>朝外标签的,吃在外面的空白,并且<面临到标签,吃在里面的空白。他们放置在标签定义的末尾,类,id和属性的声明之后,但在/或=。

http://haml.info/docs/yardoc/file.REFERENCE。 HTML#whitespace_removal__and_

在方法我已经采取了这样的事情是使用字符串插值:

I will first #{link_to 'Link somewhere'}#{', then render this half of the sentence if a condition is met' if condition}

我不喜欢在插值后的文字串的样子,但我已经与之前先前声明的字符串或动态生成的字符串中使用它。

您可以做到这一点,以保持领先的空间:

%a{:href => 'http://example.com'}>= ' link somewhere'

的空间在引号。

(&#32)

尽管没有很好的记载,这是干净地使用HAML空白保存(>)与ASCII空间组合来实现,并且不与助手:

%a{:href=>'/home'}> Home link
,&#32; 
%a{:href=>'/page'} Next link

这会产生你想要什么:

<a href='/home'>Anchor text</a>,&#32;
<a href='/page'>More text</a>

但我同意,HAML需要拿出这样做的更好的方法,因为它并不必要的ASCII字符添加到页面(但它仍然比使用助手更有效)。

有角钢支架“空白咀嚼”语法,否则写一个辅助方法它。

我碰到了类似的问题,发现这个,所以我想我会发布另一种解决方案,它不需要一个辅助方法。使用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}

这个作品在3.0.18,它也可以工作在早期版本中。

不过,我已经在过去使用的另一种选择:

- if @condition
  %span> , then some more text after the link.

您也总是可以做:

= link_to url_path do 
  = ["part_1", "part_2"].join(", ")

这我得到的工作的解决方案是:

I will first
= link_to 'link somewhere', 'http://example.com'
- if @condition
  = ", then render this half of the sentence if a condition is met"

您可以使用=,虽然=用于输出的Rails代码的结果,但在这里,它将服务器的目的。

保存为我功能工作

.white-space-pre= preserve "TEXT"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top