2
是否有一个变量包含可以从部分内部访问的部分名称?你能从部分内部得到一个Rails的部分名称吗?
render :partial => 'foo'
在_foo.haml:
.name
= partial_name # would output "foo"
是否有一个变量包含可以从部分内部访问的部分名称?你能从部分内部得到一个Rails的部分名称吗?
render :partial => 'foo'
在_foo.haml:
.name
= partial_name # would output "foo"
__FILE__
会给你的文件名
<% __FILE__.split("/").last.sub(/^_/, "") %>
在你的部分:
<%= partial_class(__FILE__) %>
在application_helper:
def partial_class(partial)
partial.split(".").first.split("/").last.sub(/^_/, "")
end
结果:partial为'_customer-existing.html.erb',输出为'customer-existing'。我经常在部分内部的包装div上使用这个名称,以便我可以在jquery中使用相同的名称来显示/隐藏部分。
实施例:
<div class='<%= partial_class(__FILE__) %>'>
stuff here that will be show/hideable by partial name.
</div>
谢谢,我知道关于'__FILE__'变量。我希望有一些像'partial_counter'这样的本地特色。 – Aaron