How can I use a condition to decide whether to output a surrounding tag in HAML? I'm trying to create the DRY version of the code below.
如何使用条件来决定是否在HAML中输出周围的标签?我正在尝试创建下面代码的DRY版本。
- if i_should_link
%a{:href => url}
.foo
.block
.of
.code
- else
.foo
.block
.of
.code
2 个解决方案
#1
29
You could use a partial.
你可以使用部分。
foo.html.haml
foo.html.haml
- if i_should_link
%a{:href => url}
= render 'bar'
- else
= render 'bar'
_bar.html.haml
_bar.html.haml
.foo
.block
.of
.code
Edit: Or you could use content for, I guess this is better because it keeps it all in the same file.
编辑:或者您可以使用内容,我想这更好,因为它将所有内容保存在同一个文件中。
- if i_should_link
%a{:href => url}
= yield :foobar
- else
= yield :foobar
- content_for :foobar do
.foo
.block
.of
.code
#2
22
I think odin's suggestion to use a partial is probably the best in most situations.
我认为在大多数情况下,odin建议使用部分可能是最好的。
However, as an alternate solution, I found a thread where Nathan Weizenbaum suggested defining this method:
但是,作为替代解决方案,我找到了Nathan Weizenbaum建议定义此方法的线程:
def haml_tag_if(condition, *args, &block)
if condition
haml_tag *args, &block
else
yield
end
end
Whatever is in the block would always be rendered, but the wrapping tag would appear or not based on the condition.
无论块中的内容是什么都会被渲染,但是包装标签会根据条件显示或不显示。
You would use it as follows:
您可以按如下方式使用它:
- haml_tag_if(planning_to_mail?, :div, :id => 'envelope') do
%p I'm a letter
If planning_to_mail?
evaluates true
, you'd get:
如果planning_to_mail?评估为真,你会得到:
<div id="envelope">
<p>I'm a letter</p>
</div>
If it evaluates false
, you'd get:
如果它评估为false,你会得到:
<p>I'm a letter</p>
He floated the idea of adding this to Haml::Helpers
, but that doesn't appear to have happened yet.
他提出了将这个添加到Haml :: Helpers的想法,但这似乎还没有发生。
#1
29
You could use a partial.
你可以使用部分。
foo.html.haml
foo.html.haml
- if i_should_link
%a{:href => url}
= render 'bar'
- else
= render 'bar'
_bar.html.haml
_bar.html.haml
.foo
.block
.of
.code
Edit: Or you could use content for, I guess this is better because it keeps it all in the same file.
编辑:或者您可以使用内容,我想这更好,因为它将所有内容保存在同一个文件中。
- if i_should_link
%a{:href => url}
= yield :foobar
- else
= yield :foobar
- content_for :foobar do
.foo
.block
.of
.code
#2
22
I think odin's suggestion to use a partial is probably the best in most situations.
我认为在大多数情况下,odin建议使用部分可能是最好的。
However, as an alternate solution, I found a thread where Nathan Weizenbaum suggested defining this method:
但是,作为替代解决方案,我找到了Nathan Weizenbaum建议定义此方法的线程:
def haml_tag_if(condition, *args, &block)
if condition
haml_tag *args, &block
else
yield
end
end
Whatever is in the block would always be rendered, but the wrapping tag would appear or not based on the condition.
无论块中的内容是什么都会被渲染,但是包装标签会根据条件显示或不显示。
You would use it as follows:
您可以按如下方式使用它:
- haml_tag_if(planning_to_mail?, :div, :id => 'envelope') do
%p I'm a letter
If planning_to_mail?
evaluates true
, you'd get:
如果planning_to_mail?评估为真,你会得到:
<div id="envelope">
<p>I'm a letter</p>
</div>
If it evaluates false
, you'd get:
如果它评估为false,你会得到:
<p>I'm a letter</p>
He floated the idea of adding this to Haml::Helpers
, but that doesn't appear to have happened yet.
他提出了将这个添加到Haml :: Helpers的想法,但这似乎还没有发生。