Reputation: 14039
Is there a variable that contains a partial's name accessible from within the partial?
render partial: 'foo'
In _foo.haml
:
.name
= partial_name # would output "foo"
Upvotes: 2
Views: 1727
Reputation: 8225
You can use a helper in application_helper for this and use rubys introspection abilities so you dont have to explicitly pass FILE like in Mikes answer.
def foo
caller_locations(1, 1).first.path
end
Upvotes: 0
Reputation: 105
In your partial:
<%= partial_class(__FILE__) %>
In application_helper:
def partial_class(partial)
partial.split(".").first.split("/").last.sub(/^_/, "")
end
Result: partial is '_customer-existing.html.erb', output is 'customer-existing'. I use this constantly for class names on a wrapper div inside the partial, so that I can use the same name in jquery to show/hide the partial.
Example:
<div class='<%= partial_class(__FILE__) %>'>
stuff here that will be show/hideable by partial name.
</div>
Upvotes: 1
Reputation: 29369
__FILE__
will give you the file name
<% __FILE__.split("/").last.sub(/^_/, "") %>
Upvotes: 5