ruby-on-rails - Haml:控制文本周围的空白

标签 ruby-on-rails 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

接近的模板:

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

= 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>

回答原来的问题:

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

关于ruby-on-rails - Haml:控制文本周围的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1311428/

相关文章:

ruby-on-rails - 为单线程 Rails 应用正确设置数据库连接池 database.yml

ruby-on-rails - rails : Validates_format_of for float not working

textmate - TextMate是否有一种简单的方法来缩进HAML文件的2个空格?

javascript - 使用 jquery 更新特定类 div 中的文本

javascript - 如何为所选元素添加背景颜色

ruby-on-rails - 复选框未以 Rails 形式保持选中状态

ruby-on-rails - 无法使用带 Rails 的 PostgreSql 创建数据库

ruby-on-rails - Rails - 在部署时自动填充表中的数据 - 角色

mysql - 很难根据允许的最大连接数正确设置 RAILS_MAX_THREADS

html - 如何使用 slim 语法转义 HTML 内容中的方括号?