Haml:控制文本周围的空格 [英] Haml: Control whitespace around text

查看:63
本文介绍了Haml:控制文本周围的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails模板中,我想使用HAML完成最终的HTML来实现此效果:

In my Rails template, I'd like to accomplish final HTML to this effect using 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

但是,您可能会注意到,这会在链接和逗号之间产生一个空格.有什么实际的方法可以避免这种空白?我知道可以删除标记周围的空格的语法,但是可以将这种语法仅应用于文本吗?我真的不喜欢额外标记的解决方案来完成此任务.

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.

推荐答案

通过Haml的助手介绍了一种更好的方法:

A better way to do this has been introduced via Haml's helpers:

= 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:控制文本周围的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆