在我的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

即将发布的模板:

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的助手介绍了一种更好的方法:

围绕

= 围绕 '(', ')' do
  %a{:href => "food"} chicken
Produces:
(<a href='food'>chicken</a>)

succeed:

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

precede:

= precede '*' do
  %span.small Not really
Produces:
*<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
Produces:
I will first
<a href="http://example.com">link somewhere</a>,
then render this half of the sentence if a condition is met

Ruby-on-rails相关问答推荐

轨道强参数,怎么允许空参数?

Rails 控制台 - 更新 Gem 后的弃用通知

Rails 和 jsonb 类型jsonb不存在

Ruby/Rails 中的类方法与常量

File.open,写入和保存?

Rspec:测试实例变量的赋值

对于 Visual Studio 用户来说,最好的 Ruby on Rails 环境是什么?

在 Rails 中,如何处理多个选中的复选框,只需拆分 , 或?

从 before_action 中排除控制器

在两个 DateTime 之间迭代,步长为一小时

未定义的方法 attr_accessible

Rails 3. 构建 oauth2 提供程序

使用 AJAX 向 Rails 发送 Authenticity Token 的正确方法

按周/月/等和 ActiveRecord 分组?

我应该将全局共享部分模板放在哪个文件夹中?

设计/Rails - 如何删除特定的 Flash 消息? (登录成功)

Postgresql JSON 数据列不同

为什么 Google Oauth 在我的 Rails 应用程序中返回无效的 redirect_urI?

Ruby 安装的 RVM 不工作?

如何删除特殊字符?