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