Mat*_*chu 96 haml ruby-on-rails
在我的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
Run Code Online (Sandbox Code Playgroud)
接近的模板:
I will first
= link_to 'link somewhere', 'http://example.com'
- if @condition
, then render this half of the sentence if a condition is met
Run Code Online (Sandbox Code Playgroud)
但是,您可能会注意到这会在链接和逗号之间产生一个空格.有没有实用的方法来避免这个空白?我知道有删除标签周围空格的语法,但这种语法是否只能应用于文本?我真的不喜欢额外标记的解决方案来实现这一目标.
Boo*_*age 208
通过Haml的帮手介绍了更好的方法:
= surround '(', ')' do
%a{:href => "food"} chicken
Run Code Online (Sandbox Code Playgroud)
生产:
(<a href='food'>chicken</a>)
Run Code Online (Sandbox Code Playgroud)
click
= succeed '.' do
%a{:href=>"thing"} here
Run Code Online (Sandbox Code Playgroud)
生产:
click
<a href='thing'>here</a>.
Run Code Online (Sandbox Code Playgroud)
= precede '*' do
%span.small Not really
Run Code Online (Sandbox Code Playgroud)
生产:
*<span class='small'>Not really</span>
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
生产:
I will first
<a href="http://example.com">link somewhere</a>,
then render this half of the sentence if a condition is met
Run Code Online (Sandbox Code Playgroud)
Boo*_*age 39
您也可以使用Haml的"trim whitespace"修改器来完成此操作.>在Haml声明后插入将阻止在其周围添加空格:
I will first
%a{:href => 'http://example.com'}> link somewhere
- if @condition
, then render this half of the sentence if a condition is met
Run Code Online (Sandbox Code Playgroud)
生产:
I will first<a href='http://example.com'>link somewhere</a>, then render this half of the sentence if a condition is met
Run Code Online (Sandbox Code Playgroud)
但是,正如您所看到的,>修饰符还会删除链接前面的空白,从而删除单词和链接之间所需的空格.我还没有找到一个漂亮的方法,除了添加 到"我会先"的结尾,像这样:
I will first
%a{:href => 'http://example.com'}> link somewhere
- if @condition
, then render this half of the sentence if a condition is met
Run Code Online (Sandbox Code Playgroud)
最终产生所需的输出而没有大量难以读取的插值:
I will first <span><a href="http://example.com">link somewhere</a></span>, then render this half of the sentence if a condition is met
Run Code Online (Sandbox Code Playgroud)
Mat*_*chu 12
好的,这是我正在解决的解决方案:
def one_line(&block)
haml_concat capture_haml(&block).gsub("\n", '').gsub('\\n', "\n")
end
Run Code Online (Sandbox Code Playgroud)
I will first
- one_line do
= link_to 'link somewhere', 'http://example.com'
- if @condition
, then render this half of the sentence
\\n
if a condition is met
Run Code Online (Sandbox Code Playgroud)
这样,默认情况下会排除空格,但我仍然可以使用"\n"行明确地包含它.(它需要双反斜杠,因为否则HAML会将其解释为实际的换行符.)让我知道是否有更好的选择!
您可以使用HAML的'aligator语法'
空白删除:>和<
并且<让您更好地控制标签附近的空白.>将删除标记周围的所有空格,而<将删除标记内的所有空格.您可以将它们视为鳄鱼吃空白:>面对标签并吃掉外面的空白,然后<面对标签并吃掉内部的空白.它们放在标签定义的末尾,在类,id和属性声明之后,但在/或=之前.
http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace_removal__and_
一旦接近我采取的这种事情是使用字符串插值:
I will first #{link_to 'Link somewhere'}#{', then render this half of the sentence if a condition is met' if condition}
Run Code Online (Sandbox Code Playgroud)
我不喜欢插值中文字字符串的外观,但我之前使用它与先前声明的字符串或动态生成的字符串.
您可以这样做以保持领先的空间:
%a{:href => 'http://example.com'}>= ' link somewhere'
Run Code Online (Sandbox Code Playgroud)
这个空间在引号中.
| 归档时间: |
|
| 查看次数: |
38133 次 |
| 最近记录: |