在我的 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 http://haml.info/docs/yardoc/file.REFERENCE.html#surround
= surround '(', ')' do
%a{:href => "food"} chicken
Produces:
(<a href='food'>chicken</a>)
succeed http://haml.info/docs/yardoc/file.REFERENCE.html#succeed:
click
= succeed '.' do
%a{:href=>"thing"} here
Produces:
click
<a href='thing'>here</a>.
precede http://haml.info/docs/yardoc/file.REFERENCE.html#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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)