Forum: Ruby on Rails Extracting the name of the template I am yielding

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rob L. (Guest)
on 2009-02-26 14:54
(Received via mailing list)
Hey guys,

I'm trying to extract the name of the template I am currently to be
yielding from my layout, but with no success. Does anyone know how to do
that?

Suppose I am calling the MainController#create action. On failure to
create my erm...Main object it re-renders the 'new' template. I'd like
to know in the layout which template I am going to render so that I can
access a help system which can dynamically include help information
depending on which template I am rendering at the time. Then no matter
what controller or what action I am calling at the time, as long as I
have the template name I can display the same information.

Any ideas?

RobL
http://www.robl.me
Gavin (Guest)
on 2009-02-26 16:17
(Received via mailing list)
Hey Rob -
I think I understand the question

Is this what you're looking for:

template will return the full name of the current template

if you stick it in your layout it'll just render the name of the
layout.

If you stick it in each template (a little more work) then it'll
return the name of the template.

Id stick this in an application_helper like so:

def this_temp
    template.to_s.split(".")[0]
end

This hacks of the filetype extention (html.erb) leaving just the name

Calling this in the view will give you
<%= this_temp %> => mains/new

Hope that helps?
Rob L. (Guest)
on 2009-02-26 18:19
(Received via mailing list)
Thanks for the help Gavin, it wasn't quite what I was looking for. Its a
bit of a weird one. Given a particular layout I want to know the name of
the template that is used in the

<%= yield %>

Which is a bit weird, I know. I would imagine that the results of the
yield are just a big string, by which time its a bit late to work out
what the name of the template I just rendered was.

Hmmzzz. Thanks all the same :)

RobL
http://www.robl.me
Gavin (Guest)
on 2009-02-26 18:25
(Received via mailing list)
Could you explain the problem in a little more depth?

Maybe there's another way to achieve what you're trying to do, aside
from finding the template name?

Gav
This topic is locked and can not be replied to.